| 1 | const math = @import("math.zig"); |
| 2 | const imax = math.imax; |
| 3 | const imin = math.imin; |
| 4 | |
| 5 | fn accessSlice(comptime array: anytype) !void { |
| 6 | var slice: []const @typeInfo(@TypeOf(array)).array.child = undefined; |
| 7 | slice = &array; |
| 8 | inline for (0.., &array) |ct_index, *elem| { |
| 9 | var rt_index: usize = undefined; |
| 10 | rt_index = ct_index; |
| 11 | if (&(slice.ptr + ct_index)[0] != elem) return error.Unexpected; |
| 12 | if (&(slice.ptr + rt_index)[0] != elem) return error.Unexpected; |
| 13 | if (&slice.ptr[ct_index..][0] != elem) return error.Unexpected; |
| 14 | if (&slice.ptr[rt_index..][0] != elem) return error.Unexpected; |
| 15 | if (&slice.ptr[ct_index] != elem) return error.Unexpected; |
| 16 | if (&slice.ptr[rt_index] != elem) return error.Unexpected; |
| 17 | if (&slice[ct_index..].ptr[0] != elem) return error.Unexpected; |
| 18 | if (&slice[rt_index..].ptr[0] != elem) return error.Unexpected; |
| 19 | if (&slice[ct_index] != elem) return error.Unexpected; |
| 20 | if (&slice[rt_index] != elem) return error.Unexpected; |
| 21 | if (slice.ptr[ct_index] != elem.*) return error.Unexpected; |
| 22 | if (slice.ptr[rt_index] != elem.*) return error.Unexpected; |
| 23 | if (slice[ct_index] != elem.*) return error.Unexpected; |
| 24 | if (slice[rt_index] != elem.*) return error.Unexpected; |
| 25 | } |
| 26 | } |
| 27 | test accessSlice { |
| 28 | try accessSlice([3]u8{ 0xdb, 0xef, 0xbd }); |
| 29 | try accessSlice([3]u16{ 0x340e, 0x3654, 0x88d7 }); |
| 30 | try accessSlice([3]u32{ 0xd424c2c0, 0x2d6ac466, 0x5a0cfaba }); |
| 31 | try accessSlice([3]u64{ |
| 32 | 0x9327a4f5221666a6, |
| 33 | 0x5c34d3ddd84a8b12, |
| 34 | 0xbae087f39f649260, |
| 35 | }); |
| 36 | try accessSlice([3]u128{ |
| 37 | 0x601cf010065444d4d42d5536dd9b95db, |
| 38 | 0xa03f592fcaa22d40af23a0c735531e3c, |
| 39 | 0x5da44907b31602b95c2d93f0b582ceab, |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | fn accessVector(comptime init: anytype) !void { |
| 44 | const Vector = @TypeOf(init); |
| 45 | const Elem = @typeInfo(Vector).vector.child; |
| 46 | const ct_vals: [2]Elem = switch (Elem) { |
| 47 | bool => .{ false, true }, |
| 48 | else => .{ imin(Elem), imax(Elem) }, |
| 49 | }; |
| 50 | var rt_vals: [2]Elem = undefined; |
| 51 | rt_vals = ct_vals; |
| 52 | var vector: Vector = undefined; |
| 53 | vector = init; |
| 54 | inline for (0..@typeInfo(Vector).vector.len) |ct_index| { |
| 55 | if (&vector[ct_index] != &vector[ct_index]) return error.Unexpected; |
| 56 | if (vector[ct_index] != init[ct_index]) return error.Unexpected; |
| 57 | vector[ct_index] = rt_vals[0]; |
| 58 | if (vector[ct_index] != ct_vals[0]) return error.Unexpected; |
| 59 | vector[ct_index] = ct_vals[1]; |
| 60 | if (vector[ct_index] != ct_vals[1]) return error.Unexpected; |
| 61 | vector[ct_index] = ct_vals[0]; |
| 62 | if (vector[ct_index] != ct_vals[0]) return error.Unexpected; |
| 63 | vector[ct_index] = rt_vals[1]; |
| 64 | if (vector[ct_index] != ct_vals[1]) return error.Unexpected; |
| 65 | } |
| 66 | } |
| 67 | test accessVector { |
| 68 | try accessVector(@Vector(1, bool){ |
| 69 | false, |
| 70 | }); |
| 71 | try accessVector(@Vector(2, bool){ |
| 72 | false, true, |
| 73 | }); |
| 74 | try accessVector(@Vector(3, bool){ |
| 75 | true, true, false, |
| 76 | }); |
| 77 | try accessVector(@Vector(5, bool){ |
| 78 | true, false, true, false, true, |
| 79 | }); |
| 80 | try accessVector(@Vector(7, bool){ |
| 81 | true, false, true, true, true, false, true, |
| 82 | }); |
| 83 | try accessVector(@Vector(8, bool){ |
| 84 | false, true, false, true, false, false, false, true, |
| 85 | }); |
| 86 | try accessVector(@Vector(9, bool){ |
| 87 | true, true, false, true, false, false, false, false, |
| 88 | true, |
| 89 | }); |
| 90 | try accessVector(@Vector(15, bool){ |
| 91 | false, true, true, true, false, true, false, false, |
| 92 | true, true, false, false, true, false, false, |
| 93 | }); |
| 94 | try accessVector(@Vector(16, bool){ |
| 95 | true, true, false, true, false, false, false, false, |
| 96 | false, true, true, false, false, false, true, true, |
| 97 | }); |
| 98 | try accessVector(@Vector(17, bool){ |
| 99 | true, false, true, true, false, true, false, true, |
| 100 | true, true, true, false, false, false, true, true, |
| 101 | false, |
| 102 | }); |
| 103 | try accessVector(@Vector(31, bool){ |
| 104 | true, false, true, true, false, true, true, true, |
| 105 | false, true, false, true, false, true, true, true, |
| 106 | false, false, true, false, false, false, false, true, |
| 107 | true, true, true, false, false, false, false, |
| 108 | }); |
| 109 | try accessVector(@Vector(32, bool){ |
| 110 | true, true, false, false, false, true, true, true, |
| 111 | false, true, true, true, false, true, false, true, |
| 112 | false, true, false, true, false, true, true, false, |
| 113 | false, false, false, false, false, true, true, true, |
| 114 | }); |
| 115 | try accessVector(@Vector(33, bool){ |
| 116 | true, false, false, false, false, true, true, true, |
| 117 | false, false, true, false, true, true, false, true, |
| 118 | true, true, false, true, true, false, false, false, |
| 119 | false, true, false, false, false, true, true, false, |
| 120 | false, |
| 121 | }); |
| 122 | try accessVector(@Vector(63, bool){ |
| 123 | false, false, true, true, true, false, true, true, |
| 124 | true, false, true, true, true, false, true, false, |
| 125 | true, true, false, true, false, true, true, true, |
| 126 | false, false, true, false, false, false, false, true, |
| 127 | true, true, true, true, false, true, false, true, |
| 128 | true, true, false, false, true, false, false, true, |
| 129 | false, true, false, false, false, false, true, true, |
| 130 | false, true, false, false, true, true, true, |
| 131 | }); |
| 132 | try accessVector(@Vector(64, bool){ |
| 133 | false, false, true, true, true, false, true, true, |
| 134 | true, false, true, true, false, true, true, false, |
| 135 | false, false, false, false, true, true, false, true, |
| 136 | true, true, true, true, false, false, false, true, |
| 137 | true, false, true, true, false, false, true, false, |
| 138 | false, true, true, false, true, true, false, false, |
| 139 | true, true, false, true, false, true, true, true, |
| 140 | false, true, true, false, false, false, false, false, |
| 141 | }); |
| 142 | try accessVector(@Vector(65, bool){ |
| 143 | false, false, true, true, true, true, true, true, |
| 144 | true, false, false, false, false, true, true, false, |
| 145 | true, false, true, true, true, false, false, false, |
| 146 | true, false, true, true, false, true, true, true, |
| 147 | true, true, false, true, true, false, true, false, |
| 148 | false, true, false, true, false, false, true, false, |
| 149 | true, false, true, true, true, false, true, true, |
| 150 | false, false, true, true, true, true, false, false, |
| 151 | true, |
| 152 | }); |
| 153 | try accessVector(@Vector(8, u8){ |
| 154 | 0x60, 0xf7, 0xf4, 0xb0, 0x05, 0xd3, 0x06, 0x78, |
| 155 | }); |
| 156 | try accessVector(@Vector(8, u16){ |
| 157 | 0x9c91, 0xfb8b, 0x7f80, 0x8304, 0x6e52, 0xd8ef, 0x37fc, 0x7851, |
| 158 | }); |
| 159 | try accessVector(@Vector(8, u32){ |
| 160 | 0x688b88e2, 0x68e2b7a2, 0x87574680, 0xab4f0769, |
| 161 | 0x75472bb5, 0xa791f2ae, 0xeb2ed416, 0x5f05ce82, |
| 162 | }); |
| 163 | try accessVector(@Vector(8, u64){ |
| 164 | 0xdefd1ddffaedf818, 0x91c78a29d3d59890, |
| 165 | 0x842aaf8fd3c7b785, 0x970a07b8f9f4a6b3, |
| 166 | 0x21b2425d1a428246, 0xea50e41174a7977b, |
| 167 | 0x08d0f1c4f5978b74, 0x8dc88a7fd85e0e67, |
| 168 | }); |
| 169 | try accessVector(@Vector(8, u128){ |
| 170 | 0x6f2cbde1fb219b1e73d7f774d10f0d94, |
| 171 | 0x7c1412616cda20436d7106691d8ba4cc, |
| 172 | 0x4ee940b50e97675b3b35d7872a35b5ad, |
| 173 | 0x6d994fb8caa1b2fac48acbb68fa2d2f1, |
| 174 | 0xdee698c7ec8de9b5940903e3fc665b63, |
| 175 | 0x0751491a509e4a1ce8cfa6d62fe9e74c, |
| 176 | 0x3d880f0a927ce3bfc2682b72070fcd50, |
| 177 | 0x82f0eec62881598699eeb93fbb456e95, |
| 178 | }); |
| 179 | try accessVector(@Vector(8, u256){ |
| 180 | 0x6ee4f35fe624d365952f73960791238ac781bfba782abc7866a691063e43ce48, |
| 181 | 0xb006491f54a9c9292458a5835b7d5f4cfa18136f175eef0a13bb8adf5c3dc061, |
| 182 | 0xd6e25ca1bc5685fc52609e261b9065bc05a8662e9291660033dd7f6d98e562b3, |
| 183 | 0x992c5e54e0e6331dac258996be7dae9b2a2eff323a39043ba8d2721420dc5f5c, |
| 184 | 0x257313f45fb3556d0fc323d5f38c953e9a093fe2278655312b6a5b64aab9d901, |
| 185 | 0x6c8ad2182b9a3b2b19c2c9b152956b383d0fee2e3fbd5b02ed72227446a7b221, |
| 186 | 0xd80cafc2252b289793799675e43f97ba4a5448c7b57e1544a464687b435efc7b, |
| 187 | 0xfcb480f2d70afd53c4689dd3f5db7638c24302f2a6a15f738167db090d91fb28, |
| 188 | }); |
| 189 | } |