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 / core / hive / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/hive/test.om: Comprehensive pure-Omega test suite for Sovereign Agent Hive Engine. |
| 3 | note Covers axial/cube coordinate identities, hex distance properties, pixel round-trip, |
| 4 | note all 6 canonical neighbors, ring sizes (6*k) and spiral enumeration, auto-placement, |
| 5 | note multi-hop shortest-path routing around obstacles, task lease/capacity/reclaim, |
| 6 | note nearest-agent assignment, and fail-closed negative controls. |
| 7 | note Compose with: si/directives/om2.om core/hive/lib.om core/hive/test.om |
| 8 | |
| 9 | text s_pass "PASS " |
| 10 | text s_fail "FAIL " |
| 11 | text s_nl "\n" |
| 12 | |
| 13 | text s_t1 "axial_cube_zero_sum_and_dist_identities" |
| 14 | text s_t2 "pixel_roundtrip_and_detail_tiers" |
| 15 | text s_t3 "all_six_canonical_neighbors" |
| 16 | text s_t4 "ring_sizes_6k_and_spiral_uniqueness" |
| 17 | text s_t5 "auto_placement_ring_expansion_and_bounds" |
| 18 | text s_t6 "multihop_routing_open_and_around_obstacles" |
| 19 | text s_t7 "task_leases_capacity_and_reclaim" |
| 20 | text s_t8 "assign_nearest_reachable_agent" |
| 21 | text s_t9 "refuse_oob_collision_dup_blocked_capacity" |
| 22 | text s_t10 "refuse_unreachable_blocked_oob_routes" |
| 23 | text s_sum "HIVE_TEST_PASS total=10 failed=0\n" |
| 24 | text s_fsum "HIVE_TEST_FAIL\n" |
| 25 | |
| 26 | fn h_report name view uchar * ok bool to uint |
| 27 | if ok |
| 28 | w1 = hive_write_all 1 s_pass |
| 29 | w2 = hive_write_all 1 name |
| 30 | w3 = hive_write_all 1 s_nl |
| 31 | if and w1 and w2 w3 |
| 32 | ret 0 |
| 33 | end |
| 34 | ret 1 |
| 35 | end |
| 36 | w1 = hive_write_all 1 s_fail |
| 37 | w2 = hive_write_all 1 name |
| 38 | w3 = hive_write_all 1 s_nl |
| 39 | if and w1 and w2 w3 |
| 40 | ret 1 |
| 41 | end |
| 42 | ret 1 |
| 43 | end |
| 44 | |
| 45 | fn test_axial_cube_dist to bool |
| 46 | ca = new CubeCoord 0 0 0 |
| 47 | cb = new CubeCoord 0 0 0 |
| 48 | cc = new CubeCoord 0 0 0 |
| 49 | hive_axial_to_cube 0 0 ca |
| 50 | if not hive_cube_valid ca |
| 51 | ret false |
| 52 | end |
| 53 | hive_axial_to_cube -3 2 ca |
| 54 | hive_axial_to_cube 4 -1 cb |
| 55 | hive_axial_to_cube 1 3 cc |
| 56 | if not and hive_cube_valid ca and hive_cube_valid cb hive_cube_valid cc |
| 57 | ret false |
| 58 | end |
| 59 | if ne hive_hex_dist 0 0 0 0 0 |
| 60 | ret false |
| 61 | end |
| 62 | if ne hive_hex_dist 0 0 1 0 1 |
| 63 | ret false |
| 64 | end |
| 65 | if ne hive_hex_dist 0 0 1 -1 1 |
| 66 | ret false |
| 67 | end |
| 68 | if ne hive_hex_dist 0 0 2 2 4 |
| 69 | ret false |
| 70 | end |
| 71 | dab = hive_hex_dist -3 2 4 -1 |
| 72 | dba = hive_hex_dist 4 -1 -3 2 |
| 73 | if ne dab 7 |
| 74 | ret false |
| 75 | end |
| 76 | if ne dab dba |
| 77 | ret false |
| 78 | end |
| 79 | if ne dab hive_cube_dist ca cb |
| 80 | ret false |
| 81 | end |
| 82 | dac = hive_hex_dist -3 2 1 3 |
| 83 | dbc = hive_hex_dist 4 -1 1 3 |
| 84 | if ne dac hive_cube_dist ca cc |
| 85 | ret false |
| 86 | end |
| 87 | if ne dbc hive_cube_dist cb cc |
| 88 | ret false |
| 89 | end |
| 90 | ret le dac add dab dbc |
| 91 | end |
| 92 | |
| 93 | fn check_px_rt q int r int to bool |
| 94 | pt = new HexCoord 0 0 |
| 95 | px = hive_axial_to_pixel_x q r HIVE_DEFAULT_RADIUS |
| 96 | py = hive_axial_to_pixel_y r HIVE_DEFAULT_RADIUS |
| 97 | if not hive_pixel_to_axial px py HIVE_DEFAULT_RADIUS pt |
| 98 | ret false |
| 99 | end |
| 100 | if ne get pt q q |
| 101 | ret false |
| 102 | end |
| 103 | ret eq get pt r r |
| 104 | end |
| 105 | |
| 106 | fn test_pixel_and_tiers to bool |
| 107 | pt = new HexCoord 0 0 |
| 108 | if not check_px_rt 0 0 |
| 109 | ret false |
| 110 | end |
| 111 | if not check_px_rt 1 0 |
| 112 | ret false |
| 113 | end |
| 114 | if not check_px_rt 1 -1 |
| 115 | ret false |
| 116 | end |
| 117 | if not check_px_rt -4 3 |
| 118 | ret false |
| 119 | end |
| 120 | if not check_px_rt 7 -2 |
| 121 | ret false |
| 122 | end |
| 123 | if hive_pixel_to_axial 0.0f64 0.0f64 0.0f64 pt |
| 124 | ret false |
| 125 | end |
| 126 | if hive_pixel_to_axial 0.0f64 0.0f64 -10.0f64 pt |
| 127 | ret false |
| 128 | end |
| 129 | if ne hive_detail_tier 1.0f64 HIVE_TIER_FULL |
| 130 | ret false |
| 131 | end |
| 132 | if ne hive_detail_tier 0.62f64 HIVE_TIER_FULL |
| 133 | ret false |
| 134 | end |
| 135 | if ne hive_detail_tier 0.61f64 HIVE_TIER_COMPACT |
| 136 | ret false |
| 137 | end |
| 138 | if ne hive_detail_tier 0.34f64 HIVE_TIER_COMPACT |
| 139 | ret false |
| 140 | end |
| 141 | if ne hive_detail_tier 0.33f64 HIVE_TIER_DOT |
| 142 | ret false |
| 143 | end |
| 144 | ret eq hive_detail_tier 0.10f64 HIVE_TIER_DOT |
| 145 | end |
| 146 | |
| 147 | fn test_six_neighbors to bool |
| 148 | qs = new int 6 0 |
| 149 | rs = new int 6 0 |
| 150 | bad = new HexCoord 0 0 |
| 151 | hive_neighbors 3 -2 qs rs |
| 152 | if or ne get qs 0 4 ne get rs 0 -2 |
| 153 | ret false |
| 154 | end |
| 155 | if or ne get qs 1 4 ne get rs 1 -3 |
| 156 | ret false |
| 157 | end |
| 158 | if or ne get qs 2 3 ne get rs 2 -3 |
| 159 | ret false |
| 160 | end |
| 161 | if or ne get qs 3 2 ne get rs 3 -2 |
| 162 | ret false |
| 163 | end |
| 164 | if or ne get qs 4 2 ne get rs 4 -1 |
| 165 | ret false |
| 166 | end |
| 167 | if or ne get qs 5 3 ne get rs 5 -1 |
| 168 | ret false |
| 169 | end |
| 170 | for i ulong 6 |
| 171 | if ne hive_hex_dist 3 -2 get qs i get rs i 1 |
| 172 | ret false |
| 173 | end |
| 174 | j = add i 1 |
| 175 | while lt j 6 bound 6 |
| 176 | if and eq get qs i get qs j eq get rs i get rs j |
| 177 | ret false |
| 178 | end |
| 179 | j = add j 1 |
| 180 | end |
| 181 | end |
| 182 | ret not hive_neighbor 3 -2 6 bad |
| 183 | end |
| 184 | |
| 185 | fn check_unique_pairs qs view int * rs view int * n ulong to bool |
| 186 | i = ulong 0 |
| 187 | while lt i n bound 64 |
| 188 | j = add i 1 |
| 189 | while lt j n bound 64 |
| 190 | if and eq get qs i get qs j eq get rs i get rs j |
| 191 | ret false |
| 192 | end |
| 193 | j = add j 1 |
| 194 | end |
| 195 | i = add i 1 |
| 196 | end |
| 197 | ret true |
| 198 | end |
| 199 | |
| 200 | fn test_rings_and_spiral to bool |
| 201 | ctr = new HexCoord 1 -1 |
| 202 | qs = new int 64 0 |
| 203 | rs = new int 64 0 |
| 204 | c0 = hive_ring ctr 0 qs rs |
| 205 | if ne c0 1 |
| 206 | ret false |
| 207 | end |
| 208 | if or ne get qs 0 1 ne get rs 0 -1 |
| 209 | ret false |
| 210 | end |
| 211 | k = uint 1 |
| 212 | while le k 4 bound 4 |
| 213 | cnt = hive_ring ctr k qs rs |
| 214 | if ne cnt mul k 6 |
| 215 | ret false |
| 216 | end |
| 217 | i = ulong 0 |
| 218 | while lt i ulong cnt bound 32 |
| 219 | if ne hive_hex_dist 1 -1 get qs i get rs i int k |
| 220 | ret false |
| 221 | end |
| 222 | i = add i 1 |
| 223 | end |
| 224 | if not check_unique_pairs qs rs ulong cnt |
| 225 | ret false |
| 226 | end |
| 227 | k = add k 1 |
| 228 | end |
| 229 | sp = hive_spiral ctr 3 qs rs |
| 230 | if ne sp 37 |
| 231 | ret false |
| 232 | end |
| 233 | ret check_unique_pairs qs rs 37 |
| 234 | end |
| 235 | |
| 236 | fn test_auto_placement to bool |
| 237 | pt = new HexCoord 0 0 |
| 238 | bd = new HiveBounds 0 0 0 0 0 0 |
| 239 | if ne hive_init 6 32 32 HIVE_OK |
| 240 | ret false |
| 241 | end |
| 242 | if ne hive_place_auto 1 0 HIVE_ROLE_COORD 2 pt HIVE_OK |
| 243 | ret false |
| 244 | end |
| 245 | if or ne get pt q 0 ne get pt r 0 |
| 246 | ret false |
| 247 | end |
| 248 | aid = uint 2 |
| 249 | while le aid 7 bound 6 |
| 250 | if ne hive_place_auto aid 1 HIVE_ROLE_WORKER 2 pt HIVE_OK |
| 251 | ret false |
| 252 | end |
| 253 | if ne hive_hex_dist 0 0 get pt q get pt r 1 |
| 254 | ret false |
| 255 | end |
| 256 | aid = add aid 1 |
| 257 | end |
| 258 | if ne hive_place_auto 8 1 HIVE_ROLE_RESEARCH 2 pt HIVE_OK |
| 259 | ret false |
| 260 | end |
| 261 | if ne hive_hex_dist 0 0 get pt q get pt r 2 |
| 262 | ret false |
| 263 | end |
| 264 | hive_compute_bounds bd |
| 265 | if ne get bd cnt 8 |
| 266 | ret false |
| 267 | end |
| 268 | ret eq get bd max_dist 2 |
| 269 | end |
| 270 | |
| 271 | fn check_valid_path pq view int 289 pr view int 289 hops int src view HexCoord dst view HexCoord to bool |
| 272 | if lt hops 0 |
| 273 | ret false |
| 274 | end |
| 275 | n = add ulong hops 1 |
| 276 | if ne get pq 0 get src q |
| 277 | ret false |
| 278 | end |
| 279 | if ne get pr 0 get src r |
| 280 | ret false |
| 281 | end |
| 282 | last = sub n 1 |
| 283 | if ne get pq last get dst q |
| 284 | ret false |
| 285 | end |
| 286 | if ne get pr last get dst r |
| 287 | ret false |
| 288 | end |
| 289 | i = ulong 0 |
| 290 | while lt i n bound 289 |
| 291 | if eq hive_cell_state get pq i get pr i HIVE_CELL_BLOCKED |
| 292 | ret false |
| 293 | end |
| 294 | if gt i 0 |
| 295 | prv = sub i 1 |
| 296 | if ne hive_hex_dist get pq prv get pr prv get pq i get pr i 1 |
| 297 | ret false |
| 298 | end |
| 299 | end |
| 300 | i = add i 1 |
| 301 | end |
| 302 | ret true |
| 303 | end |
| 304 | |
| 305 | fn test_routing_obstacles to bool |
| 306 | src = new HexCoord -3 0 |
| 307 | dst = new HexCoord 3 0 |
| 308 | pq = new int 289 0 |
| 309 | pr = new int 289 0 |
| 310 | if ne hive_init 6 32 32 HIVE_OK |
| 311 | ret false |
| 312 | end |
| 313 | h0 = hive_route src dst pq pr |
| 314 | if ne h0 6 |
| 315 | ret false |
| 316 | end |
| 317 | if not check_valid_path pq pr h0 src dst |
| 318 | ret false |
| 319 | end |
| 320 | note Build an obstacle wall across q = 0 for r in -2..2 |
| 321 | if ne hive_block_cell 0 -2 HIVE_OK |
| 322 | ret false |
| 323 | end |
| 324 | if ne hive_block_cell 0 -1 HIVE_OK |
| 325 | ret false |
| 326 | end |
| 327 | if ne hive_block_cell 0 0 HIVE_OK |
| 328 | ret false |
| 329 | end |
| 330 | if ne hive_block_cell 0 1 HIVE_OK |
| 331 | ret false |
| 332 | end |
| 333 | if ne hive_block_cell 0 2 HIVE_OK |
| 334 | ret false |
| 335 | end |
| 336 | put src q -2 |
| 337 | put src r 0 |
| 338 | put dst q 2 |
| 339 | put dst r 0 |
| 340 | h1 = hive_route src dst pq pr |
| 341 | if le h1 4 |
| 342 | ret false |
| 343 | end |
| 344 | if not check_valid_path pq pr h1 src dst |
| 345 | ret false |
| 346 | end |
| 347 | note Direct distance is 4; shortest detour around wall through (0,-3) or (0,3) takes 3 + 5 = 8 hops |
| 348 | ret eq h1 8 |
| 349 | end |
| 350 | |
| 351 | fn test_task_lifecycle to bool |
| 352 | if ne hive_init 6 16 16 HIVE_OK |
| 353 | ret false |
| 354 | end |
| 355 | if ne hive_place_agent 1 0 0 HIVE_ROLE_WORKER 1 HIVE_OK |
| 356 | ret false |
| 357 | end |
| 358 | if ne hive_place_agent 2 1 0 HIVE_ROLE_WORKER 2 HIVE_OK |
| 359 | ret false |
| 360 | end |
| 361 | if ne hive_task_create 1 1 -1 1 HIVE_OK |
| 362 | ret false |
| 363 | end |
| 364 | if ne hive_task_create 2 -1 1 1 HIVE_OK |
| 365 | ret false |
| 366 | end |
| 367 | if ne hive_task_claim 1 1 10 HIVE_OK |
| 368 | ret false |
| 369 | end |
| 370 | note Agent 1 has cap=1, claiming second task must fail with CAPACITY |
| 371 | if ne hive_task_claim 2 1 10 HIVE_ERR_CAPACITY |
| 372 | ret false |
| 373 | end |
| 374 | note Agent 2 trying to steal active lease on task 1 must fail with CLAIMED |
| 375 | if ne hive_task_claim 1 2 10 HIVE_ERR_CLAIMED |
| 376 | ret false |
| 377 | end |
| 378 | note Agent 2 trying to heartbeat or submit task 1 must fail with UNAUTH |
| 379 | if ne hive_task_heartbeat 1 2 20 HIVE_ERR_UNAUTH |
| 380 | ret false |
| 381 | end |
| 382 | if ne hive_task_submit 1 2 HIVE_ERR_UNAUTH |
| 383 | ret false |
| 384 | end |
| 385 | note Agent 1 heartbeats, submits, and task is verified -> releases load |
| 386 | if ne hive_task_heartbeat 1 1 20 HIVE_OK |
| 387 | ret false |
| 388 | end |
| 389 | if ne hive_task_submit 1 1 HIVE_OK |
| 390 | ret false |
| 391 | end |
| 392 | if ne hive_task_verify 1 true HIVE_OK |
| 393 | ret false |
| 394 | end |
| 395 | note Now agent 1 load is 0 and can claim task 2 with ttl=5 |
| 396 | if ne hive_task_claim 2 1 5 HIVE_OK |
| 397 | ret false |
| 398 | end |
| 399 | note Advance clock by 5 -> lease expires and is reclaimed |
| 400 | rec = hive_tick 5 |
| 401 | if ne rec 1 |
| 402 | ret false |
| 403 | end |
| 404 | note Agent 2 can now claim reclaimed task 2 |
| 405 | ret eq hive_task_claim 2 2 10 HIVE_OK |
| 406 | end |
| 407 | |
| 408 | fn test_assign_nearest to bool |
| 409 | asg = new uint 1 0 |
| 410 | if ne hive_init 6 16 16 HIVE_OK |
| 411 | ret false |
| 412 | end |
| 413 | note Agent 1 at (1, 0) but cap=1 and already busy with task 1 |
| 414 | if ne hive_place_agent 1 1 0 HIVE_ROLE_WORKER 1 HIVE_OK |
| 415 | ret false |
| 416 | end |
| 417 | note Agent 2 at (-2, 0), free |
| 418 | if ne hive_place_agent 2 -2 0 HIVE_ROLE_WORKER 2 HIVE_OK |
| 419 | ret false |
| 420 | end |
| 421 | note Agent 3 at (4, 0), free (only 2 hops from task 2 at (2, 0)!) |
| 422 | if ne hive_place_agent 3 4 0 HIVE_ROLE_WORKER 2 HIVE_OK |
| 423 | ret false |
| 424 | end |
| 425 | if ne hive_task_create 1 1 0 1 HIVE_OK |
| 426 | ret false |
| 427 | end |
| 428 | if ne hive_task_claim 1 1 50 HIVE_OK |
| 429 | ret false |
| 430 | end |
| 431 | if ne hive_task_create 2 2 0 2 HIVE_OK |
| 432 | ret false |
| 433 | end |
| 434 | if ne hive_assign_nearest 2 30 asg HIVE_OK |
| 435 | ret false |
| 436 | end |
| 437 | ret eq get asg 0 3 |
| 438 | end |
| 439 | |
| 440 | fn test_refusals_grid to bool |
| 441 | if ne hive_init 4 2 4 HIVE_OK |
| 442 | ret false |
| 443 | end |
| 444 | note OOB placement (dist=5 > radius=4) |
| 445 | if ne hive_place_agent 1 5 0 HIVE_ROLE_WORKER 1 HIVE_ERR_OOB |
| 446 | ret false |
| 447 | end |
| 448 | note Valid placement at (0, 0) |
| 449 | if ne hive_place_agent 1 0 0 HIVE_ROLE_COORD 1 HIVE_OK |
| 450 | ret false |
| 451 | end |
| 452 | note Collision on occupied cell (0, 0) |
| 453 | if ne hive_place_agent 2 0 0 HIVE_ROLE_WORKER 1 HIVE_ERR_COLLISION |
| 454 | ret false |
| 455 | end |
| 456 | note Duplicate agent ID 1 at free cell (1, 0) |
| 457 | if ne hive_place_agent 1 1 0 HIVE_ROLE_WORKER 1 HIVE_ERR_DUP_AGENT |
| 458 | ret false |
| 459 | end |
| 460 | note Blocking occupied cell refused with COLLISION |
| 461 | if ne hive_block_cell 0 0 HIVE_ERR_COLLISION |
| 462 | ret false |
| 463 | end |
| 464 | note Block (1, 0), double-block refused, placing on blocked refused |
| 465 | if ne hive_block_cell 1 0 HIVE_OK |
| 466 | ret false |
| 467 | end |
| 468 | if ne hive_block_cell 1 0 HIVE_ERR_BLOCKED |
| 469 | ret false |
| 470 | end |
| 471 | if ne hive_place_agent 2 1 0 HIVE_ROLE_WORKER 1 HIVE_ERR_BLOCKED |
| 472 | ret false |
| 473 | end |
| 474 | note Place 2nd agent at (-1, 0), then 3rd exceeds max_agents=2 -> CAPACITY |
| 475 | if ne hive_place_agent 2 -1 0 HIVE_ROLE_WORKER 1 HIVE_OK |
| 476 | ret false |
| 477 | end |
| 478 | if ne hive_place_agent 3 0 1 HIVE_ROLE_WORKER 1 HIVE_ERR_CAPACITY |
| 479 | ret false |
| 480 | end |
| 481 | note Removing agent holding claimed task refused with CLAIMED |
| 482 | if ne hive_task_create 1 -1 0 1 HIVE_OK |
| 483 | ret false |
| 484 | end |
| 485 | if ne hive_task_claim 1 2 10 HIVE_OK |
| 486 | ret false |
| 487 | end |
| 488 | ret eq hive_remove_agent 2 HIVE_ERR_CLAIMED |
| 489 | end |
| 490 | |
| 491 | fn test_refusals_routes to bool |
| 492 | src = new HexCoord 0 0 |
| 493 | dst = new HexCoord 2 0 |
| 494 | pq = new int 289 0 |
| 495 | pr = new int 289 0 |
| 496 | tiny_q = new int 2 0 |
| 497 | tiny_r = new int 2 0 |
| 498 | nqs = new int 6 0 |
| 499 | nrs = new int 6 0 |
| 500 | if ne hive_init 5 16 16 HIVE_OK |
| 501 | ret false |
| 502 | end |
| 503 | note Route to OOB cell refused (-HIVE_ERR_OOB) |
| 504 | put dst q 6 |
| 505 | put dst r 0 |
| 506 | if ne hive_route src dst pq pr neg int HIVE_ERR_OOB |
| 507 | ret false |
| 508 | end |
| 509 | note Surround (2, 0) with all 6 blocked neighbors |
| 510 | hive_neighbors 2 0 nqs nrs |
| 511 | for i ulong 6 |
| 512 | if ne hive_block_cell get nqs i get nrs i HIVE_OK |
| 513 | ret false |
| 514 | end |
| 515 | end |
| 516 | put dst q 2 |
| 517 | put dst r 0 |
| 518 | if ne hive_route src dst pq pr neg int HIVE_ERR_NO_ROUTE |
| 519 | ret false |
| 520 | end |
| 521 | note Route to a blocked cell refused (-HIVE_ERR_BLOCKED) |
| 522 | put dst q 3 |
| 523 | put dst r 0 |
| 524 | if ne hive_route src dst pq pr neg int HIVE_ERR_BLOCKED |
| 525 | ret false |
| 526 | end |
| 527 | note Undersized path buffer refused (-HIVE_ERR_CAPACITY) |
| 528 | put dst q -3 |
| 529 | put dst r 0 |
| 530 | ret eq hive_route src dst tiny_q tiny_r neg int HIVE_ERR_CAPACITY |
| 531 | end |
| 532 | |
| 533 | fn main to int |
| 534 | fails = uint 0 |
| 535 | fails = add fails h_report s_t1 test_axial_cube_dist |
| 536 | fails = add fails h_report s_t2 test_pixel_and_tiers |
| 537 | fails = add fails h_report s_t3 test_six_neighbors |
| 538 | fails = add fails h_report s_t4 test_rings_and_spiral |
| 539 | fails = add fails h_report s_t5 test_auto_placement |
| 540 | fails = add fails h_report s_t6 test_routing_obstacles |
| 541 | fails = add fails h_report s_t7 test_task_lifecycle |
| 542 | fails = add fails h_report s_t8 test_assign_nearest |
| 543 | fails = add fails h_report s_t9 test_refusals_grid |
| 544 | fails = add fails h_report s_t10 test_refusals_routes |
| 545 | if eq fails 0 |
| 546 | ok = hive_write_all 1 s_sum |
| 547 | if ok |
| 548 | ret 0 |
| 549 | end |
| 550 | ret 2 |
| 551 | end |
| 552 | ok = hive_write_all 1 s_fsum |
| 553 | if ok |
| 554 | ret 1 |
| 555 | end |
| 556 | ret 1 |
| 557 | end |