interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / test_omega_unwritten_trap.c

93 lines · 4136 bytes Raw Blame History
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/* Host unit test of the C3 unwritten-output detector, on synthetic buffers. */
18#include "omega_unwritten_trap.h"
19#include <stdio.h>
20#include <stdlib.h>
21
22#define POISON 0x55555555u
23static int pass, fail;
24static void check(int ok, const char *id) { if (ok) pass++; else fail++; printf("[%s] %s\n", ok ? "PASS" : "FAIL", id); }
25
26static uint32_t *mk(size_t n) {
27 uint32_t *b = malloc(n * 4);
28 if (!b) exit(2);
29 for (size_t i = 0; i < n; i++) b[i] = (uint32_t)(0x3f800000u + i); /* never the poison */
30 return b;
31}
32static void fillp(uint32_t *b, size_t lo, size_t hi) { for (size_t i = lo; i < hi; i++) b[i] = POISON; }
33
34int main(void) {
35 OmegaUnwrittenReport r;
36 const size_t N = 1000003; /* not a multiple of 64; last block has 3 words */
37 uint32_t *b = mk(N);
38
39 omega_unwritten_scan(b, N, POISON, 64, &r);
40 check(r.count == 0 && r.runs == 0 && !r.whole_blocks && !r.tail && r.full_blocks == 0 && r.partial_blocks == 0, "NO_POISON");
41
42 omega_unwritten_scan(b, 0, POISON, 64, &r);
43 check(r.count == 0, "EMPTY_BUFFER");
44
45 /* tail: last 100 words */
46 fillp(b, N - 100, N);
47 omega_unwritten_scan(b, N, POISON, 64, &r);
48 check(r.count == 100 && r.first == N - 100 && r.last == N - 1 && r.runs == 1 && r.tail, "TAIL_COUNT_INDEX_RUN");
49 check(!r.whole_blocks && r.partial_blocks == 1 && r.full_blocks == 2, "TAIL_STARTS_MID_BLOCK_IS_NOT_WHOLE");
50 free(b); b = mk(N);
51
52 /* tail that is exactly the short last block: whole */
53 fillp(b, N - 3, N);
54 omega_unwritten_scan(b, N, POISON, 64, &r);
55 check(r.count == 3 && r.tail && r.whole_blocks && r.full_blocks == 1, "SHORT_LAST_BLOCK_IS_WHOLE");
56 free(b);
57
58 /* whole-block window in the middle: 1536 blocks = 98304 words (the C3 event shape) */
59 size_t M = 64 * 262144 - 37; /* 16777179: grid-sized, not a multiple of 64 */
60 size_t lo = 64 * 3000, hi = lo + 98304;
61 b = mk(M);
62 fillp(b, lo, hi);
63 omega_unwritten_scan(b, M, POISON, 64, &r);
64 check(r.count == 98304 && r.first == lo && r.last == hi - 1, "WINDOW_COUNT_FIRST_LAST");
65 check(r.whole_blocks && r.full_blocks == 1536 && r.partial_blocks == 0 && r.runs == 1 && r.longest_run == 98304 && !r.tail, "WINDOW_IS_1536_WHOLE_BLOCKS");
66
67 /* two separate whole-block windows */
68 fillp(b, 64 * 10, 64 * 12);
69 omega_unwritten_scan(b, M, POISON, 64, &r);
70 check(r.runs == 2 && r.full_blocks == 1538 && r.whole_blocks && r.first == 640, "TWO_WINDOWS");
71 free(b); b = mk(N);
72
73 /* shifted window (starts 1 word into a block): not whole blocks */
74 fillp(b, 64 * 100 + 1, 64 * 102 + 1);
75 omega_unwritten_scan(b, N, POISON, 64, &r);
76 check(r.count == 128 && !r.whole_blocks && r.partial_blocks == 2 && r.full_blocks == 1, "SHIFTED_WINDOW_NOT_WHOLE");
77 free(b); b = mk(N);
78
79 /* scattered hits */
80 size_t idx[] = { 5, 70, 71, 9000, 500000, N - 1 };
81 for (size_t i = 0; i < sizeof(idx) / sizeof(idx[0]); i++) b[idx[i]] = POISON;
82 omega_unwritten_scan(b, N, POISON, 64, &r);
83 check(r.count == 6 && r.first == 5 && r.last == N - 1 && r.runs == 5 && r.longest_run == 2, "SCATTERED_COUNT_INDEX_RUNS");
84 check(!r.whole_blocks && !r.tail && r.partial_blocks == 5 && r.full_blocks == 0, "SCATTERED_NOT_WHOLE_NOT_TAIL");
85
86 /* a different poison value is ignored */
87 omega_unwritten_scan(b, N, 0xdeadbeefu, 64, &r);
88 check(r.count == 0, "OTHER_POISON_NOT_COUNTED");
89 free(b);
90
91 printf("SUMMARY passed=%d failed=%d\n", pass, fail);
92 return fail ? 1 : 0;
93}