complex_vector.adl
1 /* 2 seed = 246801357 3 min_height = 16 4 5 [case] 6 program = "vector_structs.alpha" 7 function = "test_vector_primitives_behavior" 8 input = ["1u32", "25u32"] 9 private_key = "APrivateKey1zkpH5Ne1Xfd79t61VhK7b6yaYz92yW5dbuVkiFheR7rwCDE" 10 11 [case] 12 program = "vector_structs.alpha" 13 function = "test_vector_structs_behavior" 14 input = ["0u32"] 15 private_key = "APrivateKey1zkpH5Ne1Xfd79t61VhK7b6yaYz92yW5dbuVkiFheR7rwCDE" 16 17 [case] 18 program = "vector_structs.alpha" 19 function = "test_vector_containers_behavior" 20 input = ["1u32"] 21 private_key = "APrivateKey1zkpH5Ne1Xfd79t61VhK7b6yaYz92yW5dbuVkiFheR7rwCDE" 22 */ 23 24 program vector_structs.alpha { 25 // 26 // ──────────────────────────────────────────────────────────────── 27 // Struct definitions 28 // ──────────────────────────────────────────────────────────────── 29 // 30 struct Point { 31 x: field, 32 y: field, 33 } 34 35 struct Container { 36 id: u32, 37 points: [Point; 2], 38 } 39 40 // 41 // ──────────────────────────────────────────────────────────────── 42 // Storage declarations (vectors only allowed here) 43 // ──────────────────────────────────────────────────────────────── 44 // 45 storage ids: [u32]; 46 storage points: [Point]; 47 storage containers: [Container]; 48 49 // 50 // ──────────────────────────────────────────────────────────────── 51 // Test 1 — Vector of primitives 52 // ──────────────────────────────────────────────────────────────── 53 // 54 async transition test_vector_primitives_behavior(index: u32, new_value: u32) -> Future { 55 return async { 56 // Ensure empty at start 57 ids.clear(); 58 assert(ids.len() == 0u32); 59 60 // Push initial elements 61 ids.push(10u32); 62 ids.push(20u32); 63 ids.push(30u32); 64 assert(ids.len() == 3u32); 65 66 // Dynamic index from input 67 let val_opt = ids.get(index); 68 assert(val_opt.unwrap() == 20u32); 69 70 let val = val_opt.unwrap(); 71 assert(val == 20u32); 72 73 // Use input to set a new value dynamically 74 ids.set(index, new_value); 75 assert(ids.get(index).unwrap() == new_value); 76 77 // Swap remove last element 78 let removed = ids.swap_remove(2u32); 79 assert(removed == 30u32); 80 assert(ids.len() == 2u32); 81 82 // Pop last element 83 let popped = ids.pop().unwrap(); 84 assert(popped == new_value); 85 assert(ids.len() == 1u32); 86 87 // Clear vector 88 ids.clear(); 89 assert(ids.len() == 0u32); 90 }; 91 } 92 93 // 94 // ──────────────────────────────────────────────────────────────── 95 // Test 2 — Vector of structs 96 // ──────────────────────────────────────────────────────────────── 97 // 98 async transition test_vector_structs_behavior(idx: u32) -> Future { 99 return async { 100 assert(points.len() == 0u32); 101 102 points.push(Point { x: 1field, y: 2field }); 103 points.push(Point { x: 3field, y: 4field }); 104 assert(points.len() == 2u32); 105 106 // Use index from input 107 let p = points.get(idx).unwrap(); 108 assert(p.x + p.y == (1field + 2field) || p.x + p.y == (3field + 4field)); 109 110 // Mutate element using index input 111 points.set(idx, Point { x: 9field, y: 9field }); 112 let updated = points.get(idx).unwrap(); 113 assert(updated.x == 9field); 114 assert(updated.y == 9field); 115 116 points.clear(); 117 assert(points.len() == 0u32); 118 }; 119 } 120 121 // 122 // ──────────────────────────────────────────────────────────────── 123 // Test 3 — Vector of containers (structs with arrays) 124 // ──────────────────────────────────────────────────────────────── 125 // 126 async transition test_vector_containers_behavior(idx: u32) -> Future { 127 return async { 128 assert(containers.len() == 0u32); 129 130 let c1 = Container { 131 id: 1u32, 132 points: [Point { x: 11field, y: 22field }, Point { x: 33field, y: 44field }], 133 }; 134 135 let c2 = Container { 136 id: 2u32, 137 points: [Point { x: 55field, y: 66field }, Point { x: 77field, y: 88field }], 138 }; 139 140 containers.push(c1); 141 containers.push(c2); 142 assert(containers.len() == 2u32); 143 144 // Use index from input 145 let target = containers.get(idx).unwrap(); 146 assert(target.id == 1u32 || target.id == 2u32); 147 148 // Dynamically mutate element at input index 149 containers.set( 150 idx, 151 Container { 152 id: 99u32, 153 points: [Point { x: 999field, y: 999field }, Point { x: 33field, y: 33field }], 154 }, 155 ); 156 157 let updated = containers.get(idx).unwrap(); 158 assert(updated.id == 99u32); 159 assert(updated.points[0].x == 999field); 160 161 containers.clear(); 162 assert(containers.len() == 0u32); 163 }; 164 } 165 166 @noupgrade 167 async constructor() {} 168 }