authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 01:21:03 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 01:21:03 -08:00
log5f26f79d2fff2bb758249bb0bde925e2268a7150
tree366a65fa8365f0c23c5a111daa696b3873467c4b
parent9dbbd77fe92435e810bf1da88ba964c1b6852823

use rawIntBytes in other tests


6 files changed, 19 insertions(+), 19 deletions(-)

src/BufIndexer.zig+5-5
......@@ -2,7 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44const indexBufferT = extras.indexBufferT;
5const rawInt = extras.rawInt;
5const rawIntBytes = extras.rawIntBytes;
66
77pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
88 return struct {
......@@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
2626}
2727
2828test {
29 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));
29 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
3030 const bindex = BufIndexer(u64, .Big).init(&bytes, 1);
3131 try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132);
3232}
3333
3434test {
35 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));
35 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
3636 const bindex = BufIndexer(u32, .Big).init(&bytes, 2);
3737 try std.testing.expect(bindex.at(0) == 0x4e5a7da9);
3838 try std.testing.expect(bindex.at(1) == 0xf3f1d132);
3939}
4040
4141test {
42 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));
42 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
4343 const bindex = BufIndexer(u16, .Big).init(&bytes, 4);
4444 try std.testing.expect(bindex.at(0) == 0x4e5a);
4545 try std.testing.expect(bindex.at(1) == 0x7da9);
......@@ -48,7 +48,7 @@ test {
4848}
4949
5050test {
51 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));
51 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
5252 const bindex = BufIndexer(u8, .Big).init(&bytes, 8);
5353 try std.testing.expect(bindex.at(0) == 0x4e);
5454 try std.testing.expect(bindex.at(1) == 0x5a);
src/hashBytes.zig+4-4
......@@ -1,7 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;
4const rawIntBytes = extras.rawIntBytes;
55
66pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 {
77 var h = Algo.init(.{});
......@@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8
1313
1414test {
1515 const A = std.crypto.hash.Md5;
16 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u128, 0x5d41402abc4b2a76b9719d911017c592))));
16 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592)));
1717}
1818
1919test {
2020 const A = std.crypto.hash.Sha1;
2121 // by default the array len is 24 for u160
22 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), std.mem.toBytes(rawInt(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))[0..20]));
22 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d)));
2323}
2424
2525test {
2626 const A = std.crypto.hash.sha2.Sha256;
27 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824))));
27 try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)));
2828}
src/indexBufferT.zig+3-3
......@@ -2,7 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44const readType = extras.readType;
5const rawInt = extras.rawInt;
5const rawIntBytes = extras.rawIntBytes;
66
77pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T {
88 std.debug.assert(idx < max_len);
......@@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En
1313}
1414
1515test {
16 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
16 const bytes = rawIntBytes(u32, 0x4e5a7da9);
1717 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a);
1818 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9);
1919}
2020test {
21 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
21 const bytes = rawIntBytes(u32, 0x4e5a7da9);
2222 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e);
2323 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d);
2424}
src/rawIntBytes.zig+1-1
......@@ -3,7 +3,7 @@ const string = []const u8;
33const extras = @import("./lib.zig");
44const rawInt = extras.rawInt;
55
6pub fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 {
6pub inline fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 {
77 return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*;
88}
99
src/readType.zig+4-4
......@@ -3,7 +3,7 @@ const string = []const u8;
33const extras = @import("./lib.zig");
44const builtin = @import("builtin");
55const native_endian = builtin.target.cpu.arch.endian();
6const rawInt = extras.rawInt;
6const rawIntBytes = extras.rawIntBytes;
77
88pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T {
99 if (T == u8) return reader.readByte(); // single bytes dont have an endianness
......@@ -56,7 +56,7 @@ test {
5656}
5757
5858test {
59 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
59 const bytes = rawIntBytes(u32, 0x4e5a7da9);
6060 var fba = std.io.fixedBufferStream(&bytes);
6161 const S = struct {
6262 a: u16,
......@@ -70,7 +70,7 @@ test {
7070}
7171
7272test {
73 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
73 const bytes = rawIntBytes(u32, 0x4e5a7da9);
7474 var fba = std.io.fixedBufferStream(&bytes);
7575 const A = [2]u16;
7676 const a = try readType(fba.reader(), A, .Big);
......@@ -79,7 +79,7 @@ test {
7979}
8080
8181test {
82 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
82 const bytes = rawIntBytes(u32, 0x4e5a7da9);
8383 var fba = std.io.fixedBufferStream(&bytes);
8484 const S = packed struct {
8585 a: u16,
src/to_hex.zig+2-2
......@@ -1,7 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;
4const rawIntBytes = extras.rawIntBytes;
55
66pub fn to_hex(array: anytype) [array.len * 2]u8 {
77 var res: [array.len * 2]u8 = undefined;
......@@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {
1111}
1212
1313test {
14 try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132"));
14 try std.testing.expect(std.mem.eql(u8, &to_hex(rawIntBytes(u64, 0x4e5a7da9f3f1d132)), "4e5a7da9f3f1d132"));
1515}