| ... | ... | @@ -213,119 +213,3 @@ test "@intCast > 128 bits" { |
| 213 | 213 | try testIntCast(u64, maxInt(u64), i255, maxInt(u64)); |
| 214 | 214 | try testIntCast(u128, maxInt(u128), i255, maxInt(u128)); |
| 215 | 215 | } |
| 216 | | |
| 217 | | const Piece = packed struct { |
| 218 | | color: Color, |
| 219 | | type: Type, |
| 220 | | |
| 221 | | const Type = enum(u3) { KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN }; |
| 222 | | const Color = enum(u1) { WHITE, BLACK }; |
| 223 | | |
| 224 | | fn charToPiece(c: u8) !@This() { |
| 225 | | return .{ |
| 226 | | .type = try charToPieceType(c), |
| 227 | | .color = if (std.ascii.isUpper(c)) Color.WHITE else Color.BLACK, |
| 228 | | }; |
| 229 | | } |
| 230 | | |
| 231 | | fn charToPieceType(c: u8) !Type { |
| 232 | | return switch (std.ascii.toLower(c)) { |
| 233 | | 'p' => .PAWN, |
| 234 | | 'k' => .KING, |
| 235 | | 'q' => .QUEEN, |
| 236 | | 'b' => .BISHOP, |
| 237 | | 'n' => .KNIGHT, |
| 238 | | 'r' => .ROOK, |
| 239 | | else => error.UnexpectedCharError, |
| 240 | | }; |
| 241 | | } |
| 242 | | }; |
| 243 | | |
| 244 | | // Originally reported at https://github.com/ziglang/zig/issues/14200 |
| 245 | | test "load non byte-sized optional value" { |
| 246 | | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 247 | | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 248 | | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO |
| 249 | | |
| 250 | | // note: this bug is triggered by the == operator, expectEqual will hide it |
| 251 | | const opt: ?Piece = try Piece.charToPiece('p'); |
| 252 | | try expect(opt.?.type == .PAWN); |
| 253 | | try expect(opt.?.color == .BLACK); |
| 254 | | |
| 255 | | var p: Piece = undefined; |
| 256 | | @as(*u8, @ptrCast(&p)).* = 0b11111011; |
| 257 | | try expect(p.type == .PAWN); |
| 258 | | try expect(p.color == .BLACK); |
| 259 | | } |
| 260 | | |
| 261 | | test "load non byte-sized value in struct" { |
| 262 | | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 263 | | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 264 | | if (builtin.cpu.arch.endian() != .little) return error.SkipZigTest; // packed struct TODO |
| 265 | | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO |
| 266 | | |
| 267 | | // note: this bug is triggered by the == operator, expectEqual will hide it |
| 268 | | // using ptrCast not to depend on unitialised memory state |
| 269 | | |
| 270 | | var struct0: struct { |
| 271 | | p: Piece, |
| 272 | | int: u8, |
| 273 | | } = undefined; |
| 274 | | @as(*u8, @ptrCast(&struct0.p)).* = 0b11111011; |
| 275 | | try expect(struct0.p.type == .PAWN); |
| 276 | | try expect(struct0.p.color == .BLACK); |
| 277 | | |
| 278 | | var struct1: packed struct { |
| 279 | | p0: Piece, |
| 280 | | p1: Piece, |
| 281 | | pad: u1, |
| 282 | | p2: Piece, |
| 283 | | } = undefined; |
| 284 | | @as(*u8, @ptrCast(&struct1.p0)).* = 0b11111011; |
| 285 | | struct1.p1 = try Piece.charToPiece('p'); |
| 286 | | struct1.p2 = try Piece.charToPiece('p'); |
| 287 | | try expect(struct1.p0.type == .PAWN); |
| 288 | | try expect(struct1.p0.color == .BLACK); |
| 289 | | try expect(struct1.p1.type == .PAWN); |
| 290 | | try expect(struct1.p1.color == .BLACK); |
| 291 | | try expect(struct1.p2.type == .PAWN); |
| 292 | | try expect(struct1.p2.color == .BLACK); |
| 293 | | } |
| 294 | | |
| 295 | | test "load non byte-sized value in union" { |
| 296 | | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 297 | | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 298 | | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 299 | | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; |
| 300 | | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 301 | | |
| 302 | | // note: this bug is triggered by the == operator, expectEqual will hide it |
| 303 | | // using ptrCast not to depend on unitialised memory state |
| 304 | | |
| 305 | | var union0: packed union { |
| 306 | | p: packed struct(u8) { |
| 307 | | a: Piece, |
| 308 | | b: u4, |
| 309 | | }, |
| 310 | | int: u8, |
| 311 | | } = .{ .int = 0 }; |
| 312 | | union0.int = 0b11111011; |
| 313 | | try expect(union0.p.a.type == .PAWN); |
| 314 | | try expect(union0.p.a.color == .BLACK); |
| 315 | | |
| 316 | | var union1: union { |
| 317 | | p: packed struct(u8) { |
| 318 | | a: Piece, |
| 319 | | b: u4, |
| 320 | | }, |
| 321 | | int: u8, |
| 322 | | } = .{ .p = .{ .a = .{ .color = .WHITE, .type = .KING }, .b = 0 } }; |
| 323 | | @as(*u8, @ptrCast(&union1.p.a)).* = 0b11111011; |
| 324 | | try expect(union1.p.a.type == .PAWN); |
| 325 | | try expect(union1.p.a.color == .BLACK); |
| 326 | | |
| 327 | | var pieces: [3]Piece = undefined; |
| 328 | | @as(*u8, @ptrCast(&pieces[1])).* = 0b11111011; |
| 329 | | try expect(pieces[1].type == .PAWN); |
| 330 | | try expect(pieces[1].color == .BLACK); |
| 331 | | } |