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 / physics0 / learner / test_pd0_protocol_v2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/physics0/learner/test_pd0_protocol_v2.om: Unit tests for PD0 protocol v2 revision 2 in pure OM-2. |
| 3 | note Prints PD0_PROTOCOL_V2: PASS|FAIL. |
| 4 | |
| 5 | global pv2_checks ulong 1 |
| 6 | global pv2_fails ulong 1 |
| 7 | |
| 8 | text PV2_FAIL_PRE "FAIL PV2 #" |
| 9 | text PV2_SUM1 "test_pd0_protocol_v2: " |
| 10 | text PV2_SUM2 " checks, " |
| 11 | text PV2_SUM3 " failed\n" |
| 12 | text PV2_PASS "PD0_PROTOCOL_V2: PASS\n" |
| 13 | text PV2_FAIL "PD0_PROTOCOL_V2: FAIL\n" |
| 14 | |
| 15 | fn pv2_check cond bool id ulong to void |
| 16 | b = new uchar 64 0 |
| 17 | put pv2_checks 0 add get pv2_checks 0 ulong 1 |
| 18 | if not cond |
| 19 | put pv2_fails 0 add get pv2_fails 0 ulong 1 |
| 20 | p = pd0_buf_str edit b ulong 0 ulong 64 PV2_FAIL_PRE |
| 21 | p = pd0_buf_u64 edit b p ulong 64 id |
| 22 | p = pd0_buf_ch edit b p ulong 64 uchar 10 |
| 23 | pd0_put_bytes 1 view b p |
| 24 | end |
| 25 | ret |
| 26 | end |
| 27 | |
| 28 | fn pv2_resp_u32 off ulong to uint |
| 29 | b = new uchar 4 0 |
| 30 | for i ulong 4 |
| 31 | put b i get p0w_resp add off i |
| 32 | end |
| 33 | ret pd0_get_u32 view b ulong 0 |
| 34 | end |
| 35 | |
| 36 | fn pv2_resp_u16 off ulong to ushort |
| 37 | b = new uchar 2 0 |
| 38 | put b 0 get p0w_resp off |
| 39 | put b 1 get p0w_resp add off 1 |
| 40 | ret pd0_get_u16 view b ulong 0 |
| 41 | end |
| 42 | |
| 43 | fn pv2_resp_u64 off ulong to ulong |
| 44 | b = new uchar 8 0 |
| 45 | for i ulong 8 |
| 46 | put b i get p0w_resp add off i |
| 47 | end |
| 48 | ret pd0_get_u64 view b ulong 0 |
| 49 | end |
| 50 | |
| 51 | fn pv2_put_req_u64 off ulong v ulong to void |
| 52 | b = new uchar 8 0 |
| 53 | pd0_put_u64 edit b ulong 0 v |
| 54 | for i ulong 8 |
| 55 | put p0w_req add off i get b i |
| 56 | end |
| 57 | ret |
| 58 | end |
| 59 | |
| 60 | fn pv2_do_score w edit Pd0World d view Pd0Desc g view Pd0Gen nobs ulong ch0 uchar v0 long to ulong |
| 61 | put p0w_req 0 PD0_OP_SCORE |
| 62 | put p0w_req 1 uchar nobs |
| 63 | pos = ulong 2 |
| 64 | for j ulong nobs bound 4 |
| 65 | v = long 0 |
| 66 | if eq j ulong 0 |
| 67 | v = long 1000000 |
| 68 | end |
| 69 | pv2_put_req_u64 pos bitcast ulong v |
| 70 | pos = add pos 8 |
| 71 | end |
| 72 | put p0w_req pos uchar 2 |
| 73 | pos = add pos 1 |
| 74 | put p0w_req pos ch0 |
| 75 | pv2_put_req_u64 add pos 1 bitcast ulong v0 |
| 76 | pos = add pos 9 |
| 77 | put p0w_req pos PD0_CH_NONE |
| 78 | pv2_put_req_u64 add pos 1 ulong 0 |
| 79 | pos = add pos 9 |
| 80 | ret pd0_world_handle w d g pos |
| 81 | end |
| 82 | |
| 83 | fn pv2_after_final w edit Pd0World d view Pd0Desc g view Pd0Gen to void |
| 84 | nobs = ulong get d n_obs |
| 85 | put p0w_req 0 PD0_OP_RESET |
| 86 | put p0w_req 1 uchar nobs |
| 87 | pv2_check eq pd0_world_handle w d g add ulong 2 mul nobs 8 ulong 0 ulong 12 |
| 88 | put p0w_req 0 PD0_OP_STEP |
| 89 | pv2_check eq pd0_world_handle w d g ulong 10 ulong 0 ulong 13 |
| 90 | put p0w_req 0 PD0_OP_SHAPE |
| 91 | n = pd0_world_handle w d g ulong 1 |
| 92 | rc = pd0_gen_true_relation g get d dt_micro ulong 14 |
| 93 | nt = ulong 0 |
| 94 | ne_cnt = ulong get p0r_neq 14 |
| 95 | for e ulong ne_cnt bound 6 |
| 96 | nt = add nt ulong get p0r_eq_nt p0r_eq_idx 14 e |
| 97 | end |
| 98 | pv2_check eq ulong get p0w_resp 11 ne_cnt ulong 14 |
| 99 | pv2_check eq ulong pv2_resp_u16 ulong 20 nt ulong 15 |
| 100 | n = pv2_do_score w d g nobs uchar 0 long 100000 |
| 101 | pv2_check eq n add ulong 2 mul ulong 16 nobs ulong 16 |
| 102 | pv2_check eq get p0w_resp 0 PD0_OK ulong 16 |
| 103 | pv2_check eq get p0w_resp 1 uchar 2 ulong 16 |
| 104 | put p0w_req 0 PD0_OP_AUDIT |
| 105 | n = pd0_world_handle w d g ulong 1 |
| 106 | pv2_check eq get p0w_resp 8 uchar 1 ulong 17 |
| 107 | pv2_check eq pv2_resp_u32 ulong 9 uint 1 ulong 17 |
| 108 | pv2_check eq pv2_resp_u32 ulong 17 uint 2 ulong 17 |
| 109 | pv2_check eq pv2_resp_u32 ulong 21 uint 1 ulong 17 |
| 110 | pv2_check eq get p0w_resp 25 uchar 0xa5 ulong 18 |
| 111 | put p0w_req 0 uchar 9 |
| 112 | pv2_check eq pd0_world_handle w d g ulong 1 ulong 0 ulong 19 |
| 113 | ret |
| 114 | end |
| 115 | |
| 116 | fn pv2_run_level level int to void |
| 117 | w = new Pd0World 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 118 | d = new Pd0Desc 0 0 0 0 0 0 0 0 0 0 |
| 119 | g = new Pd0Gen 0 0 0 0 0 0 0 |
| 120 | pv2_check eq pd0_world_init edit w edit d edit g level ulong 1 0 ulong 1 |
| 121 | nobs = ulong get d n_obs |
| 122 | dd = new uchar 256 0 |
| 123 | dn = pd0_desc_encode view d edit dd |
| 124 | put p0w_req 0 PD0_OP_DESCRIBE |
| 125 | n = pd0_world_handle edit w view d view g ulong 1 |
| 126 | pv2_check eq n dn ulong 2 |
| 127 | pv2_check eq pv2_do_score edit w view d view g nobs uchar 0 long 100000 ulong 0 ulong 3 |
| 128 | put p0w_req 0 PD0_OP_SHAPE |
| 129 | n = pd0_world_handle edit w view d view g ulong 1 |
| 130 | pv2_check and eq n ulong 22 and eq get p0w_resp 11 uchar 0 eq pv2_resp_u16 ulong 20 ushort 0 ulong 4 |
| 131 | pv2_check eq int pv2_resp_u32 ulong 12 pd0_gen_true_size level ulong 5 |
| 132 | put p0w_req 0 PD0_OP_AUDIT |
| 133 | n = pd0_world_handle edit w view d view g ulong 1 |
| 134 | pv2_check and eq n PD0_AUDIT_RESP_SIZE and eq get p0w_resp 8 uchar 0 eq pv2_resp_u32 ulong 9 uint 1 ulong 6 |
| 135 | put p0w_req 0 PD0_OP_RESET |
| 136 | put p0w_req 1 uchar nobs |
| 137 | for j ulong nobs bound 4 |
| 138 | v = long 0 |
| 139 | if eq j ulong 0 |
| 140 | v = long 1000000 |
| 141 | end |
| 142 | pv2_put_req_u64 add ulong 2 mul j 8 bitcast ulong v |
| 143 | end |
| 144 | n = pd0_world_handle edit w view d view g add ulong 2 mul nobs 8 |
| 145 | pv2_check gt n ulong 0 ulong 7 |
| 146 | put p0w_req 0 PD0_OP_STEP |
| 147 | put p0w_req 1 uchar 0 |
| 148 | pv2_put_req_u64 ulong 2 bitcast ulong long 100000 |
| 149 | n = pd0_world_handle edit w view d view g ulong 10 |
| 150 | pv2_check gt n ulong 0 ulong 8 |
| 151 | put p0w_req 0 PD0_OP_FINAL |
| 152 | pv2_check eq pd0_world_handle edit w view d view g ulong 5 ulong 0 ulong 9 |
| 153 | put p0w_req 0 PD0_OP_FINAL |
| 154 | for i ulong 32 |
| 155 | put p0w_req add ulong 1 i uchar 0xa5 |
| 156 | end |
| 157 | n = pd0_world_handle edit w view d view g ulong 33 |
| 158 | pv2_check and eq n ulong 1 eq get p0w_resp 0 uchar 0 ulong 10 |
| 159 | put p0w_req 0 PD0_OP_FINAL |
| 160 | for i ulong 32 |
| 161 | put p0w_req add ulong 1 i uchar 0x5a |
| 162 | end |
| 163 | n = pd0_world_handle edit w view d view g ulong 33 |
| 164 | pv2_check and eq n ulong 1 eq get p0w_resp 0 uchar 1 ulong 11 |
| 165 | pv2_after_final edit w view d view g |
| 166 | ret |
| 167 | end |
| 168 | |
| 169 | fn main to int |
| 170 | for lvl ulong 8 |
| 171 | pv2_run_level int lvl |
| 172 | end |
| 173 | obuf = new uchar 128 0 |
| 174 | p = pd0_buf_str edit obuf ulong 0 ulong 128 PV2_SUM1 |
| 175 | p = pd0_buf_u64 edit obuf p ulong 128 get pv2_checks 0 |
| 176 | p = pd0_buf_str edit obuf p ulong 128 PV2_SUM2 |
| 177 | p = pd0_buf_u64 edit obuf p ulong 128 get pv2_fails 0 |
| 178 | p = pd0_buf_str edit obuf p ulong 128 PV2_SUM3 |
| 179 | pd0_put_bytes 1 view obuf p |
| 180 | if eq get pv2_fails 0 ulong 0 |
| 181 | pd0_puts 1 PV2_PASS |
| 182 | ret 0 |
| 183 | end |
| 184 | pd0_puts 1 PV2_FAIL |
| 185 | ret 1 |
| 186 | end |