From 5f26f79d2fff2bb758249bb0bde925e2268a7150 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 29 Jan 2024 01:21:03 -0800 Subject: [PATCH] use rawIntBytes in other tests --- src/BufIndexer.zig | 10 +++++----- src/hashBytes.zig | 8 ++++---- src/indexBufferT.zig | 6 +++--- src/rawIntBytes.zig | 2 +- src/readType.zig | 8 ++++---- src/to_hex.zig | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/BufIndexer.zig b/src/BufIndexer.zig index a73dbf487e4fa927bfc845beb4b0fd7221e52f17..7a14cd1e1410c34f1523452e78d782de968c248b 100644 --- a/src/BufIndexer.zig +++ b/src/BufIndexer.zig @@ -2,7 +2,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); const indexBufferT = extras.indexBufferT; -const rawInt = extras.rawInt; +const rawIntBytes = extras.rawIntBytes; pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { return struct { @@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { } test { - const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); + const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); const bindex = BufIndexer(u64, .Big).init(&bytes, 1); try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132); } test { - const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); + const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); const bindex = BufIndexer(u32, .Big).init(&bytes, 2); try std.testing.expect(bindex.at(0) == 0x4e5a7da9); try std.testing.expect(bindex.at(1) == 0xf3f1d132); } test { - const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); + const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); const bindex = BufIndexer(u16, .Big).init(&bytes, 4); try std.testing.expect(bindex.at(0) == 0x4e5a); try std.testing.expect(bindex.at(1) == 0x7da9); @@ -48,7 +48,7 @@ test { } test { - const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); + const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); const bindex = BufIndexer(u8, .Big).init(&bytes, 8); try std.testing.expect(bindex.at(0) == 0x4e); try std.testing.expect(bindex.at(1) == 0x5a); diff --git a/src/hashBytes.zig b/src/hashBytes.zig index 0f9d7f5a11f32860901856c8992c801898687eba..315d69402e4367b84bcd00387c907fbe59f5737a 100644 --- a/src/hashBytes.zig +++ b/src/hashBytes.zig @@ -1,7 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); -const rawInt = extras.rawInt; +const rawIntBytes = extras.rawIntBytes; pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 { var h = Algo.init(.{}); @@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 test { const A = std.crypto.hash.Md5; - try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u128, 0x5d41402abc4b2a76b9719d911017c592)))); + try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592))); } test { const A = std.crypto.hash.Sha1; // by default the array len is 24 for u160 - try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), std.mem.toBytes(rawInt(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))[0..20])); + try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))); } test { const A = std.crypto.hash.sha2.Sha256; - try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)))); + try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824))); } diff --git a/src/indexBufferT.zig b/src/indexBufferT.zig index 9b0c249023fb773ce8c7741a314631a2d8561b67..039dd8b5ad70094e0b2eb129100cd8fd1f188e0a 100644 --- a/src/indexBufferT.zig +++ b/src/indexBufferT.zig @@ -2,7 +2,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); const readType = extras.readType; -const rawInt = extras.rawInt; +const rawIntBytes = extras.rawIntBytes; pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T { std.debug.assert(idx < max_len); @@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En } test { - const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); + const bytes = rawIntBytes(u32, 0x4e5a7da9); try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a); try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9); } test { - const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); + const bytes = rawIntBytes(u32, 0x4e5a7da9); try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e); try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d); } diff --git a/src/rawIntBytes.zig b/src/rawIntBytes.zig index 2ec75ae4fb2cfe14603fadc61018a094a7be471f..131ac87b8c10968f3a89b0086c982dad8479db9a 100644 --- a/src/rawIntBytes.zig +++ b/src/rawIntBytes.zig @@ -3,7 +3,7 @@ const string = []const u8; const extras = @import("./lib.zig"); const rawInt = extras.rawInt; -pub fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 { +pub inline fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 { return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*; } diff --git a/src/readType.zig b/src/readType.zig index 16f754866a4f7d47be84e24b4e8238f9cefcbe8e..58feaa94aa91746f4aa1dce73bd7721ed65a18ca 100644 --- a/src/readType.zig +++ b/src/readType.zig @@ -3,7 +3,7 @@ const string = []const u8; const extras = @import("./lib.zig"); const builtin = @import("builtin"); const native_endian = builtin.target.cpu.arch.endian(); -const rawInt = extras.rawInt; +const rawIntBytes = extras.rawIntBytes; pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T { if (T == u8) return reader.readByte(); // single bytes dont have an endianness @@ -56,7 +56,7 @@ test { } test { - const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); + const bytes = rawIntBytes(u32, 0x4e5a7da9); var fba = std.io.fixedBufferStream(&bytes); const S = struct { a: u16, @@ -70,7 +70,7 @@ test { } test { - const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); + const bytes = rawIntBytes(u32, 0x4e5a7da9); var fba = std.io.fixedBufferStream(&bytes); const A = [2]u16; const a = try readType(fba.reader(), A, .Big); @@ -79,7 +79,7 @@ test { } test { - const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); + const bytes = rawIntBytes(u32, 0x4e5a7da9); var fba = std.io.fixedBufferStream(&bytes); const S = packed struct { a: u16, diff --git a/src/to_hex.zig b/src/to_hex.zig index 354381ef88e55c493f51512db8d1930be07f1f68..d6c245a6fa8212582a2bf0f787308dc34b6487cc 100644 --- a/src/to_hex.zig +++ b/src/to_hex.zig @@ -1,7 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); -const rawInt = extras.rawInt; +const rawIntBytes = extras.rawIntBytes; pub fn to_hex(array: anytype) [array.len * 2]u8 { var res: [array.len * 2]u8 = undefined; @@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { } test { - try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132")); + try std.testing.expect(std.mem.eql(u8, &to_hex(rawIntBytes(u64, 0x4e5a7da9f3f1d132)), "4e5a7da9f3f1d132")); } -- 2.54.0