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 / gpu / head-to-head / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gpu/head-to-head/test.om: comprehensive verification tests for gpu/head-to-head/lib.om. |
| 3 | note Compose with: si/directives/om2.om gpu/head-to-head/lib.om gpu/head-to-head/test.om |
| 4 | |
| 5 | text h2h_ts_pass "PASS " |
| 6 | text h2h_ts_fail "FAIL " |
| 7 | text h2h_ts_nl "\n" |
| 8 | text h2h_ts_sum_ok "HEAD_TO_HEAD_TEST PASS passed=" |
| 9 | text h2h_ts_sum_fl "HEAD_TO_HEAD_TEST FAIL passed=" |
| 10 | text h2h_ts_fld " failed=" |
| 11 | |
| 12 | text h2h_tn_1 "exact_parity" |
| 13 | text h2h_tn_2 "tolerance_boundary_pass" |
| 14 | text h2h_tn_3 "tolerance_boundary_fail" |
| 15 | text h2h_tn_4 "nan_rejection_both_arms" |
| 16 | text h2h_tn_5 "inf_rejection_pos_and_neg" |
| 17 | text h2h_tn_6 "stats_sort_odd_even_p95" |
| 18 | text h2h_tn_7 "speedup_bandwidth_iqr_calls" |
| 19 | text h2h_tn_8 "mutant_control_caught_pass" |
| 20 | text h2h_tn_9 "mutant_control_escaped_fail" |
| 21 | text h2h_tn_10 "not_run_and_zero_timing_refusal" |
| 22 | text h2h_tn_11 "number_parser_u64_f64_sci" |
| 23 | |
| 24 | text h2h_tp_u64 "123456789" |
| 25 | text h2h_tp_f1 "-42.125" |
| 26 | text h2h_tp_f2 "1.5e-3" |
| 27 | text h2h_tp_bad "12.3.4" |
| 28 | |
| 29 | fn h2h_f64_near a f64 b f64 eps f64 to bool |
| 30 | if not h2h_f64_is_finite a |
| 31 | ret false |
| 32 | end |
| 33 | if not h2h_f64_is_finite b |
| 34 | ret false |
| 35 | end |
| 36 | ret le fabs sub a b eps |
| 37 | end |
| 38 | |
| 39 | fn h2h_rec_case name view uchar * ok bool tally edit ulong 2 to void |
| 40 | w = false |
| 41 | if ok |
| 42 | put tally 0 add get tally 0 1 |
| 43 | w = h2h_write_all 1 h2h_ts_pass |
| 44 | else |
| 45 | put tally 1 add get tally 1 1 |
| 46 | w = h2h_write_all 1 h2h_ts_fail |
| 47 | end |
| 48 | w = and w h2h_write_all 1 name |
| 49 | w = and w h2h_write_all 1 h2h_ts_nl |
| 50 | if w |
| 51 | ret |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | fn test_exact_parity to bool |
| 56 | a = new f64 4 0.0f64 |
| 57 | b = new f64 4 0.0f64 |
| 58 | put a 0 1.25f64 |
| 59 | put a 1 -3.5f64 |
| 60 | put a 2 0.0f64 |
| 61 | put a 3 1024.75f64 |
| 62 | put b 0 1.25f64 |
| 63 | put b 1 -3.5f64 |
| 64 | put b 2 0.0f64 |
| 65 | put b 3 1024.75f64 |
| 66 | tol = new H2hTol 0.0f64 0.0f64 |
| 67 | res = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 68 | h2h_check_parity a b tol res |
| 69 | if ne get res ok 1 |
| 70 | ret false |
| 71 | end |
| 72 | if ne get res elem_n 4 |
| 73 | ret false |
| 74 | end |
| 75 | if ne get res mismatches 0 |
| 76 | ret false |
| 77 | end |
| 78 | if ne get res nan_cnt 0 |
| 79 | ret false |
| 80 | end |
| 81 | if ne get res inf_cnt 0 |
| 82 | ret false |
| 83 | end |
| 84 | if ne get res max_abs 0.0f64 |
| 85 | ret false |
| 86 | end |
| 87 | ret eq get res max_rel 0.0f64 |
| 88 | end |
| 89 | |
| 90 | fn test_tol_pass to bool |
| 91 | a = new f64 2 0.0f64 |
| 92 | b = new f64 2 0.0f64 |
| 93 | note atol=1e-4, rtol=1e-3: at 0.0 limit=1e-4; at 100.0 limit=1e-4 + 0.1 = 0.1001 |
| 94 | put a 0 0.0f64 |
| 95 | put b 0 0.00009f64 |
| 96 | put a 1 100.0f64 |
| 97 | put b 1 100.05f64 |
| 98 | tol = new H2hTol 0.0001f64 0.001f64 |
| 99 | res = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 100 | h2h_check_parity a b tol res |
| 101 | if ne get res ok 1 |
| 102 | ret false |
| 103 | end |
| 104 | if ne get res mismatches 0 |
| 105 | ret false |
| 106 | end |
| 107 | if not h2h_f64_near get res max_abs 0.05f64 0.000001f64 |
| 108 | ret false |
| 109 | end |
| 110 | ret gt get res max_rel 0.0f64 |
| 111 | end |
| 112 | |
| 113 | fn test_tol_fail to bool |
| 114 | a = new f64 2 0.0f64 |
| 115 | b = new f64 2 0.0f64 |
| 116 | note atol=1e-4, rtol=1e-3: at 100.0 limit=0.1001; diff=0.2 must FAIL |
| 117 | put a 0 1.0f64 |
| 118 | put b 0 1.0f64 |
| 119 | put a 1 100.0f64 |
| 120 | put b 1 100.2f64 |
| 121 | tol = new H2hTol 0.0001f64 0.001f64 |
| 122 | res = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 123 | h2h_check_parity a b tol res |
| 124 | if ne get res ok 0 |
| 125 | ret false |
| 126 | end |
| 127 | if ne get res mismatches 1 |
| 128 | ret false |
| 129 | end |
| 130 | ret h2h_f64_near get res max_abs 0.2f64 0.000001f64 |
| 131 | end |
| 132 | |
| 133 | fn test_nan_rejection to bool |
| 134 | a = new f64 3 1.0f64 |
| 135 | b = new f64 3 1.0f64 |
| 136 | qnan = bitcast f64 ulong 0x7ff8000000000000 |
| 137 | put b 1 qnan |
| 138 | note Even with huge tolerances, NaN MUST be refused |
| 139 | tol = new H2hTol 1000000.0f64 1000000.0f64 |
| 140 | res = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 141 | h2h_check_parity a b tol res |
| 142 | if ne get res ok 0 |
| 143 | ret false |
| 144 | end |
| 145 | if ne get res nan_cnt 1 |
| 146 | ret false |
| 147 | end |
| 148 | if ne get res mismatches 1 |
| 149 | ret false |
| 150 | end |
| 151 | put a 0 qnan |
| 152 | h2h_check_parity a b tol res |
| 153 | if ne get res ok 0 |
| 154 | ret false |
| 155 | end |
| 156 | if ne get res nan_cnt 2 |
| 157 | ret false |
| 158 | end |
| 159 | ret eq get res mismatches 2 |
| 160 | end |
| 161 | |
| 162 | fn test_inf_rejection to bool |
| 163 | a = new f64 2 2.0f64 |
| 164 | b = new f64 2 2.0f64 |
| 165 | pinf = bitcast f64 ulong 0x7ff0000000000000 |
| 166 | ninf = bitcast f64 ulong 0xfff0000000000000 |
| 167 | put a 0 pinf |
| 168 | put b 0 pinf |
| 169 | put b 1 ninf |
| 170 | tol = new H2hTol 1000.0f64 1000.0f64 |
| 171 | res = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 172 | h2h_check_parity a b tol res |
| 173 | if ne get res ok 0 |
| 174 | ret false |
| 175 | end |
| 176 | if ne get res inf_cnt 3 |
| 177 | ret false |
| 178 | end |
| 179 | ret eq get res mismatches 2 |
| 180 | end |
| 181 | |
| 182 | fn test_stats_sort to bool |
| 183 | note Unsorted 20 values: 100..2000 step 100 shuffled |
| 184 | raw = new ulong 20 0 |
| 185 | scr = new ulong 20 0 |
| 186 | put raw 0 ulong 2000 |
| 187 | put raw 1 ulong 100 |
| 188 | put raw 2 ulong 1500 |
| 189 | put raw 3 ulong 400 |
| 190 | put raw 4 ulong 1900 |
| 191 | put raw 5 ulong 200 |
| 192 | put raw 6 ulong 1800 |
| 193 | put raw 7 ulong 300 |
| 194 | put raw 8 ulong 1700 |
| 195 | put raw 9 ulong 500 |
| 196 | put raw 10 ulong 1600 |
| 197 | put raw 11 ulong 600 |
| 198 | put raw 12 ulong 1400 |
| 199 | put raw 13 ulong 700 |
| 200 | put raw 14 ulong 1300 |
| 201 | put raw 15 ulong 800 |
| 202 | put raw 16 ulong 1200 |
| 203 | put raw 17 ulong 900 |
| 204 | put raw 18 ulong 1100 |
| 205 | put raw 19 ulong 1000 |
| 206 | st = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 207 | if not h2h_compute_stats raw scr st |
| 208 | ret false |
| 209 | end |
| 210 | if ne get st min_ns 100 |
| 211 | ret false |
| 212 | end |
| 213 | if ne get st max_ns 2000 |
| 214 | ret false |
| 215 | end |
| 216 | note Even n=20: median = (1000 + 1100) / 2 = 1050.0 |
| 217 | if not h2h_f64_near get st med_ns 1050.0f64 0.000001f64 |
| 218 | ret false |
| 219 | end |
| 220 | note Nearest-rank for n=20: q1=idx 4 (500), q3=idx 14 (1500), iqr=1000, p95=idx 18 (1900) |
| 221 | if ne get st q1_ns 500 |
| 222 | ret false |
| 223 | end |
| 224 | if ne get st q3_ns 1500 |
| 225 | ret false |
| 226 | end |
| 227 | if ne get st iqr_ns 1000 |
| 228 | ret false |
| 229 | end |
| 230 | if ne get st p95_ns 1900 |
| 231 | ret false |
| 232 | end |
| 233 | note Odd n=5: [50, 10, 40, 20, 30] -> median=30.0, p95=50 |
| 234 | odd = new ulong 5 0 |
| 235 | put odd 0 ulong 50 |
| 236 | put odd 1 ulong 10 |
| 237 | put odd 2 ulong 40 |
| 238 | put odd 3 ulong 20 |
| 239 | put odd 4 ulong 30 |
| 240 | sto = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 241 | if not h2h_compute_stats odd scr sto |
| 242 | ret false |
| 243 | end |
| 244 | if not h2h_f64_near get sto med_ns 30.0f64 0.000001f64 |
| 245 | ret false |
| 246 | end |
| 247 | ret eq get sto p95_ns 50 |
| 248 | end |
| 249 | |
| 250 | fn test_speedup_bw_iqr to bool |
| 251 | ta = new ulong 4 0 |
| 252 | tb = new ulong 4 0 |
| 253 | scr = new ulong 4 0 |
| 254 | put ta 0 ulong 2000000 |
| 255 | put ta 1 ulong 2000000 |
| 256 | put ta 2 ulong 2000000 |
| 257 | put ta 3 ulong 2000000 |
| 258 | put tb 0 ulong 500000 |
| 259 | put tb 1 ulong 500000 |
| 260 | put tb 2 ulong 500000 |
| 261 | put tb 3 ulong 500000 |
| 262 | sa = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 263 | sb = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 264 | oka = h2h_compute_stats ta scr sa |
| 265 | okb = h2h_compute_stats tb scr sb |
| 266 | if not and oka okb |
| 267 | ret false |
| 268 | end |
| 269 | sp = h2h_speedup get sa med_ns get sb med_ns |
| 270 | if not h2h_f64_near sp 4.0f64 0.000001f64 |
| 271 | ret false |
| 272 | end |
| 273 | note 4000000 bytes in 500000 ns = 8.0 GB/s; in 2000000 ns = 2.0 GB/s |
| 274 | bwa = h2h_bw_gbps ulong 4000000 get sa med_ns |
| 275 | bwb = h2h_bw_gbps ulong 4000000 get sb med_ns |
| 276 | if not h2h_f64_near bwa 2.0f64 0.000001f64 |
| 277 | ret false |
| 278 | end |
| 279 | if not h2h_f64_near bwb 8.0f64 0.000001f64 |
| 280 | ret false |
| 281 | end |
| 282 | if ne h2h_iqr_call sa sb H2H_CALL_B_FASTER |
| 283 | ret false |
| 284 | end |
| 285 | if ne h2h_iqr_call sb sa H2H_CALL_A_FASTER |
| 286 | ret false |
| 287 | end |
| 288 | sc = new H2hStats 1 4 2000000 2000000 2000000.0f64 2000000 0 2000000 2000000 |
| 289 | ret eq h2h_iqr_call sa sc H2H_CALL_OVERLAPPING |
| 290 | end |
| 291 | |
| 292 | fn make_test_pair_stats sa edit H2hStats sb edit H2hStats to bool |
| 293 | ta = new ulong 3 1000000 |
| 294 | tb = new ulong 3 500000 |
| 295 | scr = new ulong 3 0 |
| 296 | oka = h2h_compute_stats ta scr sa |
| 297 | okb = h2h_compute_stats tb scr sb |
| 298 | ret and oka okb |
| 299 | end |
| 300 | |
| 301 | fn test_mutant_caught to bool |
| 302 | sa = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 303 | sb = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 304 | if not make_test_pair_stats sa sb |
| 305 | ret false |
| 306 | end |
| 307 | fa = new f64 4 2.5f64 |
| 308 | fb = new f64 4 2.5f64 |
| 309 | fm = new f64 4 0.0f64 |
| 310 | tol = new H2hTol 0.000001f64 0.00001f64 |
| 311 | par = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 312 | mpar = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 313 | h2h_check_parity fa fb tol par |
| 314 | mn = h2h_make_wrong_kernel_mutant fa fm |
| 315 | if ne mn 4 |
| 316 | ret false |
| 317 | end |
| 318 | if not h2h_verify_mutant fa fm tol mpar |
| 319 | ret false |
| 320 | end |
| 321 | sum = new H2hSummary 2 0 4096 0.0f64 0.0f64 0.0f64 0 0 0 0 |
| 322 | h2h_finalize_summary sa sb par 1 1 sum |
| 323 | if ne get sum verdict H2H_VERDICT_PASS |
| 324 | ret false |
| 325 | end |
| 326 | ret eq get sum reason H2H_REASON_OK |
| 327 | end |
| 328 | |
| 329 | fn test_mutant_escaped_fails to bool |
| 330 | sa = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 331 | sb = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 332 | if not make_test_pair_stats sa sb |
| 333 | ret false |
| 334 | end |
| 335 | fa = new f64 4 2.5f64 |
| 336 | fb = new f64 4 2.5f64 |
| 337 | note Escaping mutant: identical to reference, so parity gate does NOT reject it! |
| 338 | fm = new f64 4 2.5f64 |
| 339 | tol = new H2hTol 0.000001f64 0.00001f64 |
| 340 | par = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 341 | mpar = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 342 | h2h_check_parity fa fb tol par |
| 343 | if h2h_verify_mutant fa fm tol mpar |
| 344 | ret false |
| 345 | end |
| 346 | sum = new H2hSummary 2 0 4096 0.0f64 0.0f64 0.0f64 0 0 0 0 |
| 347 | h2h_finalize_summary sa sb par 1 0 sum |
| 348 | if ne get sum verdict H2H_VERDICT_FAIL |
| 349 | ret false |
| 350 | end |
| 351 | ret eq get sum reason H2H_REASON_MUTANT_ESCAPED |
| 352 | end |
| 353 | |
| 354 | fn test_not_run_and_zero_time to bool |
| 355 | sa = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 356 | sb = new H2hStats 0 0 0 0 0.0f64 0 0 0 0 |
| 357 | par = new H2hParity 0 0 0 0 0 0.0f64 0.0f64 |
| 358 | sum = new H2hSummary 0 0 0 0.0f64 0.0f64 0.0f64 0 0 0 0 |
| 359 | h2h_finalize_summary sa sb par 0 0 sum |
| 360 | if ne get sum verdict H2H_VERDICT_NOT_RUN |
| 361 | ret false |
| 362 | end |
| 363 | if ne get sum reason H2H_REASON_NO_PAIRS |
| 364 | ret false |
| 365 | end |
| 366 | note Zero-ns sample MUST be refused by h2h_compute_stats |
| 367 | bad = new ulong 2 0 |
| 368 | put bad 0 ulong 100 |
| 369 | put bad 1 ulong 0 |
| 370 | scr = new ulong 2 0 |
| 371 | if h2h_compute_stats bad scr sa |
| 372 | ret false |
| 373 | end |
| 374 | ret eq get sa ok 0 |
| 375 | end |
| 376 | |
| 377 | fn test_parsers to bool |
| 378 | if not h2h_parse_u64_ok h2h_tp_u64 0 len h2h_tp_u64 |
| 379 | ret false |
| 380 | end |
| 381 | if ne h2h_parse_u64_val h2h_tp_u64 0 len h2h_tp_u64 123456789 |
| 382 | ret false |
| 383 | end |
| 384 | if not h2h_parse_f64_ok h2h_tp_f1 0 len h2h_tp_f1 |
| 385 | ret false |
| 386 | end |
| 387 | if not h2h_f64_near h2h_parse_f64_val h2h_tp_f1 0 len h2h_tp_f1 -42.125f64 0.000001f64 |
| 388 | ret false |
| 389 | end |
| 390 | if not h2h_parse_f64_ok h2h_tp_f2 0 len h2h_tp_f2 |
| 391 | ret false |
| 392 | end |
| 393 | if not h2h_f64_near h2h_parse_f64_val h2h_tp_f2 0 len h2h_tp_f2 0.0015f64 0.0000001f64 |
| 394 | ret false |
| 395 | end |
| 396 | ret not h2h_parse_f64_ok h2h_tp_bad 0 len h2h_tp_bad |
| 397 | end |
| 398 | |
| 399 | fn main to int |
| 400 | tally = new ulong 2 0 |
| 401 | h2h_rec_case h2h_tn_1 test_exact_parity tally |
| 402 | h2h_rec_case h2h_tn_2 test_tol_pass tally |
| 403 | h2h_rec_case h2h_tn_3 test_tol_fail tally |
| 404 | h2h_rec_case h2h_tn_4 test_nan_rejection tally |
| 405 | h2h_rec_case h2h_tn_5 test_inf_rejection tally |
| 406 | h2h_rec_case h2h_tn_6 test_stats_sort tally |
| 407 | h2h_rec_case h2h_tn_7 test_speedup_bw_iqr tally |
| 408 | h2h_rec_case h2h_tn_8 test_mutant_caught tally |
| 409 | h2h_rec_case h2h_tn_9 test_mutant_escaped_fails tally |
| 410 | h2h_rec_case h2h_tn_10 test_not_run_and_zero_time tally |
| 411 | h2h_rec_case h2h_tn_11 test_parsers tally |
| 412 | passed = get tally 0 |
| 413 | failed = get tally 1 |
| 414 | ok = false |
| 415 | if eq failed 0 |
| 416 | ok = h2h_write_all 1 h2h_ts_sum_ok |
| 417 | else |
| 418 | ok = h2h_write_all 1 h2h_ts_sum_fl |
| 419 | end |
| 420 | h2h_write_u64 1 passed |
| 421 | ok = and ok h2h_write_all 1 h2h_ts_fld |
| 422 | h2h_write_u64 1 failed |
| 423 | ok = and ok h2h_write_all 1 h2h_ts_nl |
| 424 | if and ok eq failed 0 |
| 425 | ret 0 |
| 426 | end |
| 427 | ret 1 |
| 428 | end |