omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / test_numeric_lifecycle.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Executes the production lifecycle wrappers against a simulated M16 driver. |
| 18 | * Each scenario gets a fresh process. There is intentionally no reset API. */ |
| 19 | #include "omega_numeric_native.h" |
| 20 | #include <assert.h> |
| 21 | #include <sys/wait.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | atomic_int omega_numeric_native_state = ATOMIC_VAR_INIT(OMEGA_NUMERIC_NATIVE_IDLE); |
| 25 | static int opens, closes, waits, open_failure, close_failure, fail_wait, wrong_word; |
| 26 | int m16_native_open(M16NativeContext *ctx) { |
| 27 | (void)ctx; opens++; return open_failure ? -1 : 0; |
| 28 | } |
| 29 | int m16_native_close(M16NativeContext *ctx) { |
| 30 | (void)ctx; closes++; return close_failure ? -1 : 0; |
| 31 | } |
| 32 | int m16_native_wait_marker(volatile uint32_t *word, uint32_t want, uint64_t ms) { |
| 33 | assert(ms == 5000 || ms == 600000); |
| 34 | waits++; |
| 35 | if (waits == fail_wait) return -1; |
| 36 | *word = wrong_word ? want + 1u : want; |
| 37 | return 0; |
| 38 | } |
| 39 | /* Defined in another translation unit: the latch must be process-wide. */ |
| 40 | int numeric_other_launcher_open(void); |
| 41 | |
| 42 | static void check_blocked(void) { |
| 43 | int before = opens; |
| 44 | assert(numeric_other_launcher_open() != 0); |
| 45 | assert(opens == before); |
| 46 | } |
| 47 | static void scenario(int which) { |
| 48 | M16NativeContext ctx; |
| 49 | volatile uint32_t marker = 0; |
| 50 | if (which == 0) { |
| 51 | open_failure = 1; |
| 52 | assert(omega_numeric_native_open(&ctx) != 0); |
| 53 | assert(closes == 0); |
| 54 | open_failure = 0; |
| 55 | assert(omega_numeric_native_open(&ctx) == 0); |
| 56 | assert(omega_numeric_native_close(&ctx) == 0); |
| 57 | return; |
| 58 | } |
| 59 | assert(omega_numeric_native_open(&ctx) == 0); |
| 60 | if (which == 1) { |
| 61 | check_blocked(); /* a simultaneous launcher cannot open another seat */ |
| 62 | assert(omega_numeric_native_close(&ctx) == 0); /* pre-submit cleanup */ |
| 63 | assert(omega_numeric_native_open(&ctx) == 0); |
| 64 | assert(omega_numeric_native_close(&ctx) == 0); |
| 65 | assert(closes == 2); |
| 66 | return; |
| 67 | } |
| 68 | if (which == 2) { |
| 69 | assert(omega_numeric_native_wait(&marker, 0x44444444u, 5000) == 0); |
| 70 | assert(omega_numeric_native_wait(&marker, 6u, 5000) == 0); |
| 71 | assert(omega_numeric_native_close(&ctx) == 0); |
| 72 | assert(omega_numeric_native_open(&ctx) == 0); |
| 73 | assert(omega_numeric_native_close(&ctx) == 0); |
| 74 | return; |
| 75 | } |
| 76 | if (which == 3) { |
| 77 | close_failure = 1; |
| 78 | assert(omega_numeric_native_close(&ctx) != 0); |
| 79 | check_blocked(); |
| 80 | return; |
| 81 | } |
| 82 | if (which == 4) wrong_word = 1; |
| 83 | else fail_wait = which - 4; /* first marker, second marker, then semaphore */ |
| 84 | int failed = 0; |
| 85 | const uint32_t expected[] = {0x44444444u, 0x46464646u, 6u}; |
| 86 | for (int i = 0; i < 3; i++) { |
| 87 | if (omega_numeric_native_wait(&marker, expected[i], 600000) != 0) { |
| 88 | failed = 1; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | assert(failed); |
| 93 | assert(omega_numeric_native_uncertain()); |
| 94 | assert(omega_numeric_native_close(&ctx) != 0); |
| 95 | assert(closes == 0); /* never release a possibly running channel */ |
| 96 | omega_numeric_native_release(); /* a late cleanup cannot clear poison */ |
| 97 | check_blocked(); |
| 98 | } |
| 99 | int main(void) { |
| 100 | for (int i = 0; i < 8; i++) { |
| 101 | pid_t p = fork(); |
| 102 | assert(p >= 0); |
| 103 | if (p == 0) { scenario(i); _exit(0); } |
| 104 | int status; |
| 105 | assert(waitpid(p, &status, 0) == p); |
| 106 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 107 | fprintf(stderr, "FAIL numeric lifecycle scenario %d\n", i); |
| 108 | return 1; |
| 109 | } |
| 110 | } |
| 111 | puts("numeric lifecycle: PASS (8 simulated-driver scenarios; hardware NOT_RUN)"); |
| 112 | return 0; |
| 113 | } |