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 / turing / test_tc.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note tests/turing/test_tc.om: EXP-001 coder test suite in pure seeded OM-2. |
| 5 | note Ports 100% of tests/turing/test_tc.c (1030 checks) WITHOUT tc_produce.om. |
| 6 | note Composed after seed/ng/sha256.om, tc_pstream.om, tc_range_lib.om, tc_rans.om. |
| 7 | |
| 8 | const SH_RANDOM ulong 0 |
| 9 | const SH_SKEW_FIRST ulong 1 |
| 10 | const SH_SKEW_LAST ulong 2 |
| 11 | const SH_UNIFORM ulong 3 |
| 12 | const SH_SPIKY ulong 4 |
| 13 | |
| 14 | const PK_SAMPLE ulong 0 |
| 15 | const PK_RAREST ulong 1 |
| 16 | const PK_LAST ulong 2 |
| 17 | const PK_FIRST ulong 3 |
| 18 | |
| 19 | text TT_DET_EXT ".det" |
| 20 | text TT_USE "usage: test_tc <output prefix>\n" |
| 21 | text TT_HDR "test_tc: " |
| 22 | text TT_PAS " passed, " |
| 23 | text TT_FAI " failed\n" |
| 24 | text TT_F "FAIL: " |
| 25 | text TT_CODER " coder=" |
| 26 | text TT_N " n=" |
| 27 | text TT_BYTES " bytes=" |
| 28 | text TT_SHA " sha=" |
| 29 | text TT_TPS1 " tps1=" |
| 30 | text TT_UB " ideal_ub=" |
| 31 | text TT_RC_STAT "range carries=" |
| 32 | text TT_PM " pending_max=" |
| 33 | text TT_REF "refuse " |
| 34 | text TT_BAR " | " |
| 35 | text TT_PF "payload flips coder=" |
| 36 | text TT_PFR " refused=" |
| 37 | text TT_PFD " decoded_different=" |
| 38 | text TT_PFS " silent=" |
| 39 | text TT_LBL_RT "round_trip" |
| 40 | text TT_LBL_CAR "carries" |
| 41 | text TT_LBL_PM "pending_max" |
| 42 | text TT_LBL_FC "fail_closed" |
| 43 | text TT_LBL_G2 "g2_rule" |
| 44 | |
| 45 | global tt_pass ulong 1 |
| 46 | global tt_fail ulong 1 |
| 47 | global tt_rng ulong 1 |
| 48 | global tt_carries ulong 1 |
| 49 | global tt_pend_max ulong 1 |
| 50 | global tt_det_fd int 1 |
| 51 | global tt_arg uchar 1024 |
| 52 | |
| 53 | note Saved base fixture for fail-closed tests (K=9, n=5000) |
| 54 | global tt_pb uchar 262144 |
| 55 | global tt_pb_len ulong 1 |
| 56 | global tt_sb uchar 8192 |
| 57 | global tt_sb_len ulong 1 |
| 58 | global tt_cr uchar 65536 |
| 59 | global tt_cr_len ulong 1 |
| 60 | global tt_ca uchar 65536 |
| 61 | global tt_ca_len ulong 1 |
| 62 | global tt_orig_sym uchar 8192 |
| 63 | global tt_w ulong 16 |
| 64 | |
| 65 | fn tt_chk cond bool msg view uchar * to void |
| 66 | if cond |
| 67 | put tt_pass 0 add get tt_pass 0 1 |
| 68 | ret |
| 69 | end |
| 70 | put tt_fail 0 add get tt_fail 0 1 |
| 71 | tc_out_reset |
| 72 | tc_out_str TT_F |
| 73 | tc_out_str msg |
| 74 | tc_out_byte 10 |
| 75 | z = tc_out_flush 1 |
| 76 | end |
| 77 | |
| 78 | fn tt_rnd to ulong |
| 79 | z = add get tt_rng 0 11400714819323198485 |
| 80 | put tt_rng 0 z |
| 81 | z = mul xor z shr z 30 13787848793156543929 |
| 82 | z = mul xor z shr z 27 10723151780598845931 |
| 83 | ret xor z shr z 31 |
| 84 | end |
| 85 | |
| 86 | fn tt_make_row qoff ulong k ulong shape ulong to void |
| 87 | if eq shape SH_SKEW_FIRST |
| 88 | for x ulong sub k 1 bound 16 |
| 89 | put tc_ps_q add qoff add x 1 1 |
| 90 | end |
| 91 | put tc_ps_q qoff ushort sub TC_QONE sub k 1 |
| 92 | ret |
| 93 | end |
| 94 | if eq shape SH_SKEW_LAST |
| 95 | for x ulong sub k 1 bound 16 |
| 96 | put tc_ps_q add qoff x 1 |
| 97 | end |
| 98 | put tc_ps_q add qoff sub k 1 ushort sub TC_QONE sub k 1 |
| 99 | ret |
| 100 | end |
| 101 | if eq shape SH_UNIFORM |
| 102 | qv = div TC_QONE k |
| 103 | for x ulong k bound 16 |
| 104 | put tc_ps_q add qoff x ushort qv |
| 105 | end |
| 106 | put tc_ps_q qoff ushort add qv rem TC_QONE k |
| 107 | ret |
| 108 | end |
| 109 | tot = ulong 0 |
| 110 | for x ulong k bound 16 |
| 111 | wv = add rem tt_rnd 1000 1 |
| 112 | if eq shape SH_SPIKY |
| 113 | if eq rem tt_rnd 4 0 |
| 114 | wv = ulong 1 |
| 115 | end |
| 116 | end |
| 117 | put tt_w x wv |
| 118 | tot = add tot wv |
| 119 | end |
| 120 | if eq shape SH_SPIKY |
| 121 | idx = rem tt_rnd k |
| 122 | put tt_w idx add get tt_w idx 100000 |
| 123 | tot = ulong 0 |
| 124 | for x ulong k bound 16 |
| 125 | tot = add tot get tt_w x |
| 126 | end |
| 127 | end |
| 128 | spare = sub TC_QONE k |
| 129 | left = TC_QONE |
| 130 | for x ulong k bound 16 |
| 131 | v = add 1 div mul spare get tt_w x tot |
| 132 | put tc_ps_q add qoff x ushort v |
| 133 | left = sub left v |
| 134 | end |
| 135 | x = ulong 0 |
| 136 | while gt left 0 bound 256 |
| 137 | qv = ulong get tc_ps_q add qoff x |
| 138 | if lt qv 65535 |
| 139 | put tc_ps_q add qoff x ushort add qv 1 |
| 140 | left = sub left 1 |
| 141 | end |
| 142 | x = rem add x 1 k |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | fn tt_pick qoff ulong k ulong picker ulong to ulong |
| 147 | if eq picker PK_LAST |
| 148 | ret sub k 1 |
| 149 | end |
| 150 | if eq picker PK_FIRST |
| 151 | ret 0 |
| 152 | end |
| 153 | if eq picker PK_RAREST |
| 154 | b = ulong 0 |
| 155 | for x ulong sub k 1 bound 16 |
| 156 | xi = add x 1 |
| 157 | if lt get tc_ps_q add qoff xi get tc_ps_q add qoff b |
| 158 | b = xi |
| 159 | end |
| 160 | end |
| 161 | ret b |
| 162 | end |
| 163 | u = and tt_rnd 65535 |
| 164 | c = ulong 0 |
| 165 | for x ulong k bound 16 |
| 166 | c = add c ulong get tc_ps_q add qoff x |
| 167 | if lt u c |
| 168 | ret x |
| 169 | end |
| 170 | end |
| 171 | ret sub k 1 |
| 172 | end |
| 173 | |
| 174 | fn tt_fill_digests to void |
| 175 | for i ulong 32 |
| 176 | put tc_ps_prof i uchar and add 31 i 255 |
| 177 | put tc_ps_mod i uchar and add 62 i 255 |
| 178 | put tc_ps_dat i uchar and add 93 i 255 |
| 179 | put tc_sy_dat i uchar and add 93 i 255 |
| 180 | end |
| 181 | end |
| 182 | |
| 183 | fn tt_make_pair k ulong n ulong shape ulong picker ulong to void |
| 184 | put tc_ps_k 0 k |
| 185 | put tc_ps_n 0 n |
| 186 | put tc_sy_k 0 k |
| 187 | put tc_sy_n 0 n |
| 188 | tt_fill_digests |
| 189 | for t ulong n bound 500000 |
| 190 | qoff = mul t k |
| 191 | tt_make_row qoff k shape |
| 192 | put tc_ps_key t mul t 7 |
| 193 | put tc_ps_crumb t uint div t 1000 |
| 194 | put tc_sy_sym t uchar tt_pick qoff k picker |
| 195 | end |
| 196 | end |
| 197 | |
| 198 | fn tt_round_trip_coder c ulong sn ulong ub long to void |
| 199 | rc = int 0 |
| 200 | if eq c 1 |
| 201 | rc = tc_range_encode |
| 202 | else |
| 203 | rc = tc_rans_encode |
| 204 | end |
| 205 | tt_chk eq rc TC_OK TT_LBL_RT |
| 206 | if ne rc TC_OK |
| 207 | ret |
| 208 | end |
| 209 | cn = get tc_cb_len 0 |
| 210 | if eq c 1 |
| 211 | rc = tc_range_decode cn |
| 212 | else |
| 213 | rc = tc_rans_decode cn |
| 214 | end |
| 215 | tt_chk eq rc TC_OK TT_LBL_RT |
| 216 | if eq rc TC_OK |
| 217 | rc2 = tc_sy_serialize 1 |
| 218 | exact = false |
| 219 | if eq rc2 TC_OK |
| 220 | if eq get tc_db_len 0 sn |
| 221 | exact = true |
| 222 | for i ulong sn bound 524288 |
| 223 | if ne get tc_db i get tc_sb i |
| 224 | exact = false |
| 225 | break |
| 226 | end |
| 227 | end |
| 228 | end |
| 229 | end |
| 230 | tt_chk exact TT_LBL_RT |
| 231 | end |
| 232 | pn = get tc_ps_n 0 |
| 233 | pay_ub = mul mul sub cn TC_CODED_HEADER 8 1000000 |
| 234 | ovh_ub = sub long pay_ub ub |
| 235 | lim_ub = add long 64000000 mul long pn 10000 |
| 236 | tt_chk le ovh_ub lim_ub TT_LBL_RT |
| 237 | tc_sha_cb 0 cn |
| 238 | tc_out_reset |
| 239 | tc_out_str TT_LBL_RT |
| 240 | tc_out_str TT_CODER |
| 241 | tc_out_u64 c |
| 242 | tc_out_str TT_N |
| 243 | tc_out_u64 pn |
| 244 | tc_out_str TT_BYTES |
| 245 | tc_out_u64 cn |
| 246 | tc_out_str TT_SHA |
| 247 | tc_out_hex_sh 32 |
| 248 | tc_out_byte 10 |
| 249 | z = tc_out_flush get tt_det_fd 0 |
| 250 | end |
| 251 | |
| 252 | fn tt_round_trip to void |
| 253 | rc1 = tc_ps_serialize |
| 254 | rc2 = tc_sy_serialize 0 |
| 255 | rc3 = int -1 |
| 256 | rc4 = int -1 |
| 257 | if eq rc1 TC_OK |
| 258 | if eq rc2 TC_OK |
| 259 | rc3 = tc_ps_parse get tc_pb_len 0 |
| 260 | rc4 = tc_sy_parse get tc_sb_len 0 |
| 261 | end |
| 262 | end |
| 263 | ok = false |
| 264 | if eq rc1 TC_OK |
| 265 | if eq rc2 TC_OK |
| 266 | if eq rc3 TC_OK |
| 267 | if eq rc4 TC_OK |
| 268 | ok = true |
| 269 | end |
| 270 | end |
| 271 | end |
| 272 | end |
| 273 | tt_chk ok TT_LBL_RT |
| 274 | if not ok |
| 275 | ret |
| 276 | end |
| 277 | sn = get tc_sb_len 0 |
| 278 | pn = get tc_ps_n 0 |
| 279 | ub = tc_ideal_ub 0 pn |
| 280 | tt_round_trip_coder 1 sn ub |
| 281 | tt_round_trip_coder 2 sn ub |
| 282 | if gt pn 0 |
| 283 | if eq tc_range_encode_raw 0 pn get tc_ps_k 0 0 TC_OK |
| 284 | put tt_carries 0 add get tt_carries 0 get tc_rc_st 7 |
| 285 | if gt get tc_rc_st 8 get tt_pend_max 0 |
| 286 | put tt_pend_max 0 get tc_rc_st 8 |
| 287 | end |
| 288 | end |
| 289 | end |
| 290 | tc_out_reset |
| 291 | tc_out_str TT_LBL_RT |
| 292 | tc_out_str TT_TPS1 |
| 293 | tc_out_hex_ps_dig 32 |
| 294 | tc_out_str TT_UB |
| 295 | tc_out_i64 ub |
| 296 | tc_out_byte 10 |
| 297 | z = tc_out_flush get tt_det_fd 0 |
| 298 | end |
| 299 | |
| 300 | fn tt_test_round_trips to void |
| 301 | ns = new ulong 6 0 |
| 302 | put ns 0 0 |
| 303 | put ns 1 1 |
| 304 | put ns 2 2 |
| 305 | put ns 3 17 |
| 306 | put ns 4 1000 |
| 307 | put ns 5 20000 |
| 308 | for ki ulong 15 |
| 309 | k = add ki 2 |
| 310 | for i ulong 6 |
| 311 | tt_make_pair k get ns i SH_RANDOM PK_SAMPLE |
| 312 | tt_round_trip |
| 313 | end |
| 314 | end |
| 315 | tt_make_pair 2 50000 SH_SKEW_FIRST PK_RAREST |
| 316 | tt_round_trip |
| 317 | tt_make_pair 16 50000 SH_SKEW_FIRST PK_RAREST |
| 318 | tt_round_trip |
| 319 | tt_make_pair 2 300000 SH_SKEW_LAST PK_LAST |
| 320 | tt_round_trip |
| 321 | tt_make_pair 16 300000 SH_SKEW_LAST PK_LAST |
| 322 | tt_round_trip |
| 323 | tt_make_pair 9 300000 SH_SKEW_FIRST PK_FIRST |
| 324 | tt_round_trip |
| 325 | tt_make_pair 9 50000 SH_SKEW_LAST PK_FIRST |
| 326 | tt_round_trip |
| 327 | tt_make_pair 16 200000 SH_SPIKY PK_SAMPLE |
| 328 | tt_round_trip |
| 329 | tt_make_pair 9 200000 SH_SPIKY PK_RAREST |
| 330 | tt_round_trip |
| 331 | tt_make_pair 9 200000 SH_UNIFORM PK_SAMPLE |
| 332 | tt_round_trip |
| 333 | tt_make_pair 16 200000 SH_RANDOM PK_SAMPLE |
| 334 | tt_round_trip |
| 335 | note Mixed runs K=9, n=400000 |
| 336 | put tc_ps_k 0 9 |
| 337 | put tc_ps_n 0 400000 |
| 338 | put tc_sy_k 0 9 |
| 339 | put tc_sy_n 0 400000 |
| 340 | tt_fill_digests |
| 341 | for t ulong 400000 bound 400000 |
| 342 | qoff = mul t 9 |
| 343 | run = rem div t 997 2 |
| 344 | if ne run 0 |
| 345 | tt_make_row qoff 9 SH_SKEW_LAST |
| 346 | put tc_sy_sym t uchar tt_pick qoff 9 PK_LAST |
| 347 | else |
| 348 | tt_make_row qoff 9 SH_RANDOM |
| 349 | put tc_sy_sym t uchar tt_pick qoff 9 PK_SAMPLE |
| 350 | end |
| 351 | put tc_ps_key t 0 |
| 352 | put tc_ps_crumb t 0 |
| 353 | end |
| 354 | tt_round_trip |
| 355 | tt_chk gt get tt_carries 0 0 TT_LBL_CAR |
| 356 | tt_chk gt get tt_pend_max 0 1 TT_LBL_PM |
| 357 | tc_out_reset |
| 358 | tc_out_str TT_RC_STAT |
| 359 | tc_out_u64 get tt_carries 0 |
| 360 | tc_out_str TT_PM |
| 361 | tc_out_u64 get tt_pend_max 0 |
| 362 | tc_out_byte 10 |
| 363 | z = tc_out_flush get tt_det_fd 0 |
| 364 | end |
| 365 | |
| 366 | note ---------------------------------------------------------------- fail-closed helpers |
| 367 | |
| 368 | fn tt_redigest_pb plen ulong to void |
| 369 | body = sub plen TC_DIGEST |
| 370 | tc_ps_hash_pb body |
| 371 | for i ulong 32 |
| 372 | tc_pb_put add body i get tc_sh_out i |
| 373 | end |
| 374 | end |
| 375 | |
| 376 | fn tt_redigest_sb slen ulong to void |
| 377 | body = sub slen TC_DIGEST |
| 378 | tc_sha_init |
| 379 | tc_sha_dom TC_DOM_SY |
| 380 | for i ulong body bound 524288 |
| 381 | tc_sha_byte get tc_sb i |
| 382 | end |
| 383 | tc_sha_final |
| 384 | for i ulong 32 |
| 385 | put tc_sb add body i get tc_sh_out i |
| 386 | end |
| 387 | end |
| 388 | |
| 389 | fn tt_restore_pb to void |
| 390 | n = get tt_pb_len 0 |
| 391 | for i ulong n bound 262144 |
| 392 | tc_pb_put i get tt_pb i |
| 393 | end |
| 394 | put tc_pb_len 0 n |
| 395 | end |
| 396 | |
| 397 | fn tt_restore_sb to void |
| 398 | n = get tt_sb_len 0 |
| 399 | for i ulong n bound 8192 |
| 400 | put tc_sb i get tt_sb i |
| 401 | end |
| 402 | put tc_sb_len 0 n |
| 403 | end |
| 404 | |
| 405 | fn tt_restore_coded c ulong to ulong |
| 406 | if eq c 1 |
| 407 | n = get tt_cr_len 0 |
| 408 | for i ulong n bound 65536 |
| 409 | put tc_cb i get tt_cr i |
| 410 | end |
| 411 | put tc_cb_len 0 n |
| 412 | ret n |
| 413 | end |
| 414 | n = get tt_ca_len 0 |
| 415 | for i ulong n bound 65536 |
| 416 | put tc_cb i get tt_ca i |
| 417 | end |
| 418 | put tc_cb_len 0 n |
| 419 | ret n |
| 420 | end |
| 421 | |
| 422 | fn tt_restore_base_ps to void |
| 423 | tt_restore_pb |
| 424 | z = tc_ps_parse get tt_pb_len 0 |
| 425 | end |
| 426 | |
| 427 | fn tt_refused rc int want int to void |
| 428 | tt_chk eq rc want TT_LBL_FC |
| 429 | has_why = false |
| 430 | if eq rc TC_OK |
| 431 | has_why = true |
| 432 | else |
| 433 | if gt get tc_whylen 0 0 |
| 434 | has_why = true |
| 435 | end |
| 436 | end |
| 437 | tt_chk has_why TT_LBL_FC |
| 438 | tc_out_reset |
| 439 | tc_out_str TT_REF |
| 440 | tc_out_errname rc |
| 441 | tc_out_str TT_BAR |
| 442 | tc_out_why |
| 443 | tc_out_byte 10 |
| 444 | z = tc_out_flush get tt_det_fd 0 |
| 445 | end |
| 446 | |
| 447 | fn tt_decode_c c ulong clen ulong to int |
| 448 | tc_why_clear |
| 449 | if eq c 1 |
| 450 | ret tc_range_decode clen |
| 451 | end |
| 452 | ret tc_rans_decode clen |
| 453 | end |
| 454 | |
| 455 | fn tt_fc_base_make to void |
| 456 | tt_make_pair 9 5000 SH_RANDOM PK_SAMPLE |
| 457 | z = tc_ps_serialize |
| 458 | z = tc_sy_serialize 0 |
| 459 | pn = get tc_pb_len 0 |
| 460 | sn = get tc_sb_len 0 |
| 461 | put tt_pb_len 0 pn |
| 462 | put tt_sb_len 0 sn |
| 463 | for i ulong pn bound 262144 |
| 464 | put tt_pb i tc_pb_get i |
| 465 | end |
| 466 | for i ulong sn bound 8192 |
| 467 | put tt_sb i get tc_sb i |
| 468 | end |
| 469 | z = tc_ps_parse pn |
| 470 | z = tc_sy_parse sn |
| 471 | for i ulong 5000 |
| 472 | put tt_orig_sym i get tc_sy_sym i |
| 473 | end |
| 474 | z = tc_range_encode |
| 475 | crn = get tc_cb_len 0 |
| 476 | put tt_cr_len 0 crn |
| 477 | for i ulong crn bound 65536 |
| 478 | put tt_cr i get tc_cb i |
| 479 | end |
| 480 | z = tc_rans_encode |
| 481 | can = get tc_cb_len 0 |
| 482 | put tt_ca_len 0 can |
| 483 | for i ulong can bound 65536 |
| 484 | put tt_ca i get tc_cb i |
| 485 | end |
| 486 | end |
| 487 | |
| 488 | fn tt_fc_part1 to void |
| 489 | note Sanity + 1 (corrupted pstream) + 2 (trunc/trail) + 3 (wrong digests) |
| 490 | rb = add TC_PS_REC_FIXED 18 |
| 491 | n1 = tt_restore_coded 1 |
| 492 | r1 = tt_decode_c 1 n1 |
| 493 | n2 = tt_restore_coded 2 |
| 494 | r2 = tt_decode_c 2 n2 |
| 495 | tt_chk and eq r1 TC_OK eq r2 TC_OK TT_LBL_FC |
| 496 | note 1. corrupted probability stream |
| 497 | tt_restore_pb |
| 498 | pn = get tt_pb_len 0 |
| 499 | off = add add TC_PS_HEADER mul 3 rb 24 |
| 500 | tc_pb_put off xor tc_pb_get off 1 |
| 501 | tc_why_clear |
| 502 | tt_refused tc_ps_parse pn TC_E_DIGEST |
| 503 | note 1b. re-digested altered stream |
| 504 | tt_restore_pb |
| 505 | q0 = tc_pb_get_u16 off |
| 506 | q1 = tc_pb_get_u16 add off 2 |
| 507 | if gt q0 1 |
| 508 | tc_pb_put_u16 off sub q0 1 |
| 509 | tc_pb_put_u16 add off 2 add q1 1 |
| 510 | else |
| 511 | tc_pb_put_u16 off add q0 1 |
| 512 | tc_pb_put_u16 add off 2 sub q1 1 |
| 513 | end |
| 514 | tt_redigest_pb pn |
| 515 | tt_chk eq tc_ps_parse pn TC_OK TT_LBL_FC |
| 516 | n1 = tt_restore_coded 1 |
| 517 | tt_refused tt_decode_c 1 n1 TC_E_BINDING |
| 518 | n2 = tt_restore_coded 2 |
| 519 | tt_refused tt_decode_c 2 n2 TC_E_BINDING |
| 520 | tt_restore_base_ps |
| 521 | note 2. truncated / trailing bitstream |
| 522 | n1 = tt_restore_coded 1 |
| 523 | tt_refused tt_decode_c 1 sub n1 1 TC_E_TRUNC |
| 524 | n2 = tt_restore_coded 2 |
| 525 | tt_refused tt_decode_c 2 sub n2 1 TC_E_TRUNC |
| 526 | for ci ulong 2 |
| 527 | c = add ci 1 |
| 528 | cn = tt_restore_coded c |
| 529 | tc_cb_put_u64 48 sub sub cn TC_CODED_HEADER 1 |
| 530 | tt_refused tt_decode_c c sub cn 1 TC_E_TRUNC |
| 531 | tc_cb_put_u64 48 2 |
| 532 | tt_refused tt_decode_c c add TC_CODED_HEADER 2 TC_E_TRUNC |
| 533 | cn = tt_restore_coded c |
| 534 | put tc_cb cn 0 |
| 535 | tt_refused tt_decode_c c add cn 1 TC_E_TRAIL |
| 536 | tc_cb_put_u64 48 add sub cn TC_CODED_HEADER 1 |
| 537 | tt_refused tt_decode_c c add cn 1 TC_E_TRAIL |
| 538 | end |
| 539 | note 3. wrong model / profile / dataset digest |
| 540 | for i ulong 32 |
| 541 | put tc_exp_prof i uchar and add 31 i 255 |
| 542 | put tc_exp_mod i uchar and add 62 i 255 |
| 543 | put tc_exp_dat i uchar and add 93 i 255 |
| 544 | end |
| 545 | tc_why_clear |
| 546 | tt_refused tc_ps_expect 1 1 1 TC_OK |
| 547 | for i ulong 32 |
| 548 | put tc_exp_mod i uchar and add 3069 i 255 |
| 549 | end |
| 550 | tc_why_clear |
| 551 | tt_refused tc_ps_expect 1 1 1 TC_E_MODEL |
| 552 | for i ulong 32 |
| 553 | put tc_exp_mod i uchar and add 62 i 255 |
| 554 | put tc_exp_prof i uchar and add 3069 i 255 |
| 555 | end |
| 556 | tc_why_clear |
| 557 | tt_refused tc_ps_expect 1 1 1 TC_E_PROFILE |
| 558 | for i ulong 32 |
| 559 | put tc_exp_prof i uchar and add 31 i 255 |
| 560 | put tc_exp_dat i uchar and add 3069 i 255 |
| 561 | end |
| 562 | tc_why_clear |
| 563 | tt_refused tc_ps_expect 1 1 1 TC_E_DATASET |
| 564 | note TSY1 dataset mismatch |
| 565 | tt_restore_sb |
| 566 | sn = get tt_sb_len 0 |
| 567 | put tc_sb 24 xor get tc_sb 24 128 |
| 568 | tt_redigest_sb sn |
| 569 | tt_chk eq tc_sy_parse sn TC_OK TT_LBL_FC |
| 570 | tc_why_clear |
| 571 | tt_refused tc_range_encode TC_E_DATASET |
| 572 | tc_why_clear |
| 573 | tt_refused tc_rans_encode TC_E_DATASET |
| 574 | tt_restore_sb |
| 575 | z = tc_sy_parse sn |
| 576 | end |
| 577 | |
| 578 | fn tt_fc_part2 to void |
| 579 | note 4. malformed normalization and zero-probability entries |
| 580 | rb = add TC_PS_REC_FIXED 18 |
| 581 | pn = get tt_pb_len 0 |
| 582 | tt_restore_pb |
| 583 | off10 = add add TC_PS_HEADER mul 10 rb 24 |
| 584 | tc_pb_put_u16 off10 add tc_pb_get_u16 off10 1 |
| 585 | tt_redigest_pb pn |
| 586 | tc_why_clear |
| 587 | tt_refused tc_ps_parse pn TC_E_NORM |
| 588 | tt_restore_pb |
| 589 | tc_pb_put_u32 add add TC_PS_HEADER mul 10 rb 20 65535 |
| 590 | tt_redigest_pb pn |
| 591 | tc_why_clear |
| 592 | tt_refused tc_ps_parse pn TC_E_NORM |
| 593 | tt_restore_pb |
| 594 | off11 = add add TC_PS_HEADER mul 11 rb 24 |
| 595 | q0 = tc_pb_get_u16 off11 |
| 596 | q1 = tc_pb_get_u16 add off11 2 |
| 597 | tc_pb_put_u16 off11 0 |
| 598 | tc_pb_put_u16 add off11 2 add q1 q0 |
| 599 | tt_redigest_pb pn |
| 600 | tc_why_clear |
| 601 | tt_refused tc_ps_parse pn TC_E_ZERO |
| 602 | note in-memory streams |
| 603 | tt_make_pair 4 10 SH_RANDOM PK_SAMPLE |
| 604 | put tc_ps_q 21 ushort add ulong get tc_ps_q 21 1 |
| 605 | tc_why_clear |
| 606 | tt_refused tc_ps_serialize TC_E_NORM |
| 607 | tt_chk eq tc_range_encode_raw 0 10 4 0 TC_E_NORM TT_LBL_FC |
| 608 | tt_chk eq tc_rans_encode_raw 0 10 4 0 TC_E_NORM TT_LBL_FC |
| 609 | put tc_ps_q 21 ushort sub ulong get tc_ps_q 21 1 |
| 610 | put tc_ps_q 21 ushort add ulong get tc_ps_q 21 ulong get tc_ps_q 22 |
| 611 | put tc_ps_q 22 0 |
| 612 | tc_why_clear |
| 613 | tt_refused tc_ps_serialize TC_E_ZERO |
| 614 | tt_chk eq tc_rans_encode_raw 0 10 4 0 TC_E_ZERO TT_LBL_FC |
| 615 | tt_chk eq tc_range_encode_raw 0 10 4 0 TC_E_NORM TT_LBL_FC |
| 616 | tt_restore_base_ps |
| 617 | tt_restore_sb |
| 618 | z = tc_sy_parse get tt_sb_len 0 |
| 619 | end |
| 620 | |
| 621 | fn tt_fc_part3 to void |
| 622 | note 5. header omitted / altered + accounting |
| 623 | offs = new ulong 7 0 |
| 624 | put offs 0 0 |
| 625 | put offs 1 4 |
| 626 | put offs 2 5 |
| 627 | put offs 3 6 |
| 628 | put offs 4 8 |
| 629 | put offs 5 20 |
| 630 | put offs 6 48 |
| 631 | xrs = new uchar 7 0 |
| 632 | put xrs 0 32 |
| 633 | put xrs 1 2 |
| 634 | put xrs 2 3 |
| 635 | put xrs 3 1 |
| 636 | put xrs 4 1 |
| 637 | put xrs 5 16 |
| 638 | put xrs 6 1 |
| 639 | wants = new int 7 0 |
| 640 | put wants 0 TC_E_HEADER |
| 641 | put wants 1 TC_E_HEADER |
| 642 | put wants 2 TC_E_HEADER |
| 643 | put wants 3 TC_E_HEADER |
| 644 | put wants 4 TC_E_COUNT |
| 645 | put wants 5 TC_E_BINDING |
| 646 | put wants 6 TC_E_TRUNC |
| 647 | for ci ulong 2 |
| 648 | c = add ci 1 |
| 649 | cn = tt_restore_coded c |
| 650 | pl = sub cn TC_CODED_HEADER |
| 651 | for i ulong pl bound 65536 |
| 652 | put tc_cb i get tc_cb add TC_CODED_HEADER i |
| 653 | end |
| 654 | tt_refused tt_decode_c c pl TC_E_HEADER |
| 655 | for ai ulong 7 |
| 656 | cn = tt_restore_coded c |
| 657 | o = get offs ai |
| 658 | put tc_cb o xor get tc_cb o get xrs ai |
| 659 | w = get wants ai |
| 660 | if eq o 48 |
| 661 | if lt tc_cb_get_u64 48 sub cn TC_CODED_HEADER |
| 662 | w = TC_E_TRAIL |
| 663 | end |
| 664 | end |
| 665 | tt_refused tt_decode_c c cn w |
| 666 | end |
| 667 | cn = tt_restore_coded c |
| 668 | tt_refused tt_decode_c sub 3 c cn TC_E_HEADER |
| 669 | end |
| 670 | z = tc_range_encode_raw 0 5000 9 0 |
| 671 | tt_chk eq get tt_cr_len 0 add TC_CODED_HEADER get tc_cb_plen 0 TT_LBL_FC |
| 672 | z = tc_rans_encode_raw 0 5000 9 0 |
| 673 | tt_chk eq get tt_ca_len 0 add TC_CODED_HEADER get tc_cb_plen 0 TT_LBL_FC |
| 674 | end |
| 675 | |
| 676 | fn tt_fc_part4 to void |
| 677 | note 6. symbol count mismatch + 7. TPS1 structure + 8. payload corruption |
| 678 | rb = add TC_PS_REC_FIXED 18 |
| 679 | pn = get tt_pb_len 0 |
| 680 | sn = get tt_sb_len 0 |
| 681 | put tc_sy_n 0 4999 |
| 682 | tc_why_clear |
| 683 | tt_refused tc_range_encode TC_E_COUNT |
| 684 | tc_why_clear |
| 685 | tt_refused tc_rans_encode TC_E_COUNT |
| 686 | tt_restore_sb |
| 687 | tc_sb_put_u64 8 5001 |
| 688 | tt_redigest_sb sn |
| 689 | tc_why_clear |
| 690 | tt_refused tc_sy_parse sn TC_E_COUNT |
| 691 | tt_restore_sb |
| 692 | put tc_sb add TC_SY_HEADER 7 9 |
| 693 | tt_redigest_sb sn |
| 694 | tc_why_clear |
| 695 | tt_refused tc_sy_parse sn TC_E_SYMBOL |
| 696 | tt_restore_sb |
| 697 | z = tc_sy_parse sn |
| 698 | note 7. TPS1 structure |
| 699 | tt_restore_pb |
| 700 | tc_pb_put_u64 add TC_PS_HEADER mul 4 rb 5 |
| 701 | tt_redigest_pb pn |
| 702 | tc_why_clear |
| 703 | tt_refused tc_ps_parse pn TC_E_INDEX |
| 704 | tt_restore_pb |
| 705 | tc_pb_put_u64 12 4999 |
| 706 | tt_redigest_pb pn |
| 707 | tc_why_clear |
| 708 | tt_refused tc_ps_parse pn TC_E_FORMAT |
| 709 | tt_restore_pb |
| 710 | tc_why_clear |
| 711 | tt_refused tc_ps_parse sub pn rb TC_E_DIGEST |
| 712 | tt_restore_pb |
| 713 | tc_pb_put 4 2 |
| 714 | tt_redigest_pb pn |
| 715 | tc_why_clear |
| 716 | tt_refused tc_ps_parse pn TC_E_FORMAT |
| 717 | tt_restore_pb |
| 718 | tc_pb_put 7 15 |
| 719 | tt_redigest_pb pn |
| 720 | tc_why_clear |
| 721 | tt_refused tc_ps_parse pn TC_E_FORMAT |
| 722 | tt_restore_base_ps |
| 723 | end |
| 724 | |
| 725 | fn tt_fc_flip_one c ulong cn ulong off ulong to ulong |
| 726 | z2 = tt_restore_coded c |
| 727 | mask = uchar shl 1 rem off 8 |
| 728 | put tc_cb off xor get tc_cb off mask |
| 729 | rc = tt_decode_c c cn |
| 730 | if ne rc TC_OK |
| 731 | ret 1 |
| 732 | end |
| 733 | for i ulong 5000 |
| 734 | if ne get tc_dsy_sym i get tt_orig_sym i |
| 735 | ret 2 |
| 736 | end |
| 737 | end |
| 738 | ret 3 |
| 739 | end |
| 740 | |
| 741 | fn tt_fc_part5 to void |
| 742 | note 8. payload corruption |
| 743 | for ci ulong 2 |
| 744 | c = add ci 1 |
| 745 | cn = tt_restore_coded c |
| 746 | refused = ulong 0 |
| 747 | differ = ulong 0 |
| 748 | silent = ulong 0 |
| 749 | off = TC_CODED_HEADER |
| 750 | while lt off cn bound 65536 |
| 751 | res = tt_fc_flip_one c cn off |
| 752 | if eq res 1 |
| 753 | refused = add refused 1 |
| 754 | else |
| 755 | if eq res 2 |
| 756 | differ = add differ 1 |
| 757 | else |
| 758 | silent = add silent 1 |
| 759 | end |
| 760 | end |
| 761 | off = add off 7 |
| 762 | end |
| 763 | tt_chk eq silent 0 TT_LBL_FC |
| 764 | tt_chk gt refused 0 TT_LBL_FC |
| 765 | tc_out_reset |
| 766 | tc_out_str TT_PF |
| 767 | tc_out_u64 c |
| 768 | tc_out_str TT_PFR |
| 769 | tc_out_u64 refused |
| 770 | tc_out_str TT_PFD |
| 771 | tc_out_u64 differ |
| 772 | tc_out_str TT_PFS |
| 773 | tc_out_u64 silent |
| 774 | tc_out_byte 10 |
| 775 | z = tc_out_flush get tt_det_fd 0 |
| 776 | end |
| 777 | end |
| 778 | |
| 779 | fn tt_test_g2_rule to void |
| 780 | tt_chk eq tc_ubits_q16 43481 591903 TT_LBL_G2 |
| 781 | tt_chk eq tc_ubits_q16 46819 485194 TT_LBL_G2 |
| 782 | ok_anc = false |
| 783 | if eq tc_ubits_q16 65536 0 |
| 784 | if eq tc_ubits_q16 32768 1000000 |
| 785 | if eq tc_ubits_q16 1 16000000 |
| 786 | ok_anc = true |
| 787 | end |
| 788 | end |
| 789 | end |
| 790 | tt_chk ok_anc TT_LBL_G2 |
| 791 | bad = ulong 0 |
| 792 | for qi ulong 65536 |
| 793 | q = add qi 1 |
| 794 | e = tc_hibit q |
| 795 | m = shl q sub 62 e |
| 796 | frac = ulong 0 |
| 797 | for i ulong 32 |
| 798 | m = tc_sq62 m |
| 799 | frac = shl frac 1 |
| 800 | if ge m 9223372036854775808 |
| 801 | m = shr m 1 |
| 802 | frac = or frac 1 |
| 803 | end |
| 804 | end |
| 805 | v = sub 68719476736 or shl e 32 frac |
| 806 | ub = long shr add mul v 1000000 2147483648 32 |
| 807 | if ne ub tc_ubits_q16 q |
| 808 | if lt bad 3 |
| 809 | bad = add bad 1 |
| 810 | tt_chk false TT_LBL_G2 |
| 811 | end |
| 812 | end |
| 813 | end |
| 814 | tt_chk eq bad 0 TT_LBL_G2 |
| 815 | end |
| 816 | |
| 817 | fn main to int |
| 818 | if ne argcnt 2 |
| 819 | tc_out_reset |
| 820 | tc_out_str TT_USE |
| 821 | z = tc_out_flush 2 |
| 822 | ret 2 |
| 823 | end |
| 824 | alen = argread 1 tt_arg 0 900 |
| 825 | if le alen 0 |
| 826 | ret 2 |
| 827 | end |
| 828 | ap = ulong alen |
| 829 | for i ulong 4 |
| 830 | put tt_arg add ap i get TT_DET_EXT i |
| 831 | end |
| 832 | fd = fopen tt_arg 0 add ap 4 577 420 |
| 833 | if lt fd 0 |
| 834 | ret 2 |
| 835 | end |
| 836 | put tt_det_fd 0 int fd |
| 837 | put tt_rng 0 11400714819323198485 |
| 838 | tt_test_round_trips |
| 839 | tt_fc_base_make |
| 840 | tt_fc_part1 |
| 841 | tt_fc_part2 |
| 842 | tt_fc_part3 |
| 843 | tt_fc_part4 |
| 844 | tt_fc_part5 |
| 845 | tt_test_g2_rule |
| 846 | z = fclose get tt_det_fd 0 |
| 847 | tc_out_reset |
| 848 | tc_out_str TT_HDR |
| 849 | tc_out_u64 get tt_pass 0 |
| 850 | tc_out_str TT_PAS |
| 851 | tc_out_u64 get tt_fail 0 |
| 852 | tc_out_str TT_FAI |
| 853 | z2 = tc_out_flush 1 |
| 854 | if ne get tt_fail 0 0 |
| 855 | ret 1 |
| 856 | end |
| 857 | ret 0 |
| 858 | end |