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");...@@ -2,7 +2,7 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const indexBufferT = extras.indexBufferT;4const indexBufferT = extras.indexBufferT;
5const rawInt = extras.rawInt;5const rawIntBytes = extras.rawIntBytes;
66
7pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {7pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
8 return struct {8 return struct {
...@@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {...@@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type {
26}26}
2727
28test {28test {
29 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));29 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
30 const bindex = BufIndexer(u64, .Big).init(&bytes, 1);30 const bindex = BufIndexer(u64, .Big).init(&bytes, 1);
31 try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132);31 try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132);
32}32}
3333
34test {34test {
35 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));35 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
36 const bindex = BufIndexer(u32, .Big).init(&bytes, 2);36 const bindex = BufIndexer(u32, .Big).init(&bytes, 2);
37 try std.testing.expect(bindex.at(0) == 0x4e5a7da9);37 try std.testing.expect(bindex.at(0) == 0x4e5a7da9);
38 try std.testing.expect(bindex.at(1) == 0xf3f1d132);38 try std.testing.expect(bindex.at(1) == 0xf3f1d132);
39}39}
4040
41test {41test {
42 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));42 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
43 const bindex = BufIndexer(u16, .Big).init(&bytes, 4);43 const bindex = BufIndexer(u16, .Big).init(&bytes, 4);
44 try std.testing.expect(bindex.at(0) == 0x4e5a);44 try std.testing.expect(bindex.at(0) == 0x4e5a);
45 try std.testing.expect(bindex.at(1) == 0x7da9);45 try std.testing.expect(bindex.at(1) == 0x7da9);
...@@ -48,7 +48,7 @@ test {...@@ -48,7 +48,7 @@ test {
48}48}
4949
50test {50test {
51 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));51 const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132);
52 const bindex = BufIndexer(u8, .Big).init(&bytes, 8);52 const bindex = BufIndexer(u8, .Big).init(&bytes, 8);
53 try std.testing.expect(bindex.at(0) == 0x4e);53 try std.testing.expect(bindex.at(0) == 0x4e);
54 try std.testing.expect(bindex.at(1) == 0x5a);54 try std.testing.expect(bindex.at(1) == 0x5a);
src/hashBytes.zig+4-4
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;4const rawIntBytes = extras.rawIntBytes;
55
6pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 {6pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 {
7 var h = Algo.init(.{});7 var h = Algo.init(.{});
...@@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8...@@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8
1313
14test {14test {
15 const A = std.crypto.hash.Md5;15 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)));
17}17}
1818
19test {19test {
20 const A = std.crypto.hash.Sha1;20 const A = std.crypto.hash.Sha1;
21 // by default the array len is 24 for u16021 // 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)));
23}23}
2424
25test {25test {
26 const A = std.crypto.hash.sha2.Sha256;26 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)));
28}28}
src/indexBufferT.zig+3-3
...@@ -2,7 +2,7 @@ const std = @import("std");...@@ -2,7 +2,7 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const readType = extras.readType;4const readType = extras.readType;
5const rawInt = extras.rawInt;5const rawIntBytes = extras.rawIntBytes;
66
7pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T {7pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T {
8 std.debug.assert(idx < max_len);8 std.debug.assert(idx < max_len);
...@@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En...@@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En
13}13}
1414
15test {15test {
16 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));16 const bytes = rawIntBytes(u32, 0x4e5a7da9);
17 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a);17 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a);
18 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9);18 try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9);
19}19}
20test {20test {
21 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));21 const bytes = rawIntBytes(u32, 0x4e5a7da9);
22 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e);22 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e);
23 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d);23 try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d);
24}24}
src/rawIntBytes.zig+1-1
...@@ -3,7 +3,7 @@ const string = []const u8;...@@ -3,7 +3,7 @@ const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;4const 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 {
7 return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*;7 return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*;
8}8}
99
src/readType.zig+4-4
...@@ -3,7 +3,7 @@ const string = []const u8;...@@ -3,7 +3,7 @@ const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const builtin = @import("builtin");4const builtin = @import("builtin");
5const native_endian = builtin.target.cpu.arch.endian();5const native_endian = builtin.target.cpu.arch.endian();
6const rawInt = extras.rawInt;6const rawIntBytes = extras.rawIntBytes;
77
8pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T {8pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T {
9 if (T == u8) return reader.readByte(); // single bytes dont have an endianness9 if (T == u8) return reader.readByte(); // single bytes dont have an endianness
...@@ -56,7 +56,7 @@ test {...@@ -56,7 +56,7 @@ test {
56}56}
5757
58test {58test {
59 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));59 const bytes = rawIntBytes(u32, 0x4e5a7da9);
60 var fba = std.io.fixedBufferStream(&bytes);60 var fba = std.io.fixedBufferStream(&bytes);
61 const S = struct {61 const S = struct {
62 a: u16,62 a: u16,
...@@ -70,7 +70,7 @@ test {...@@ -70,7 +70,7 @@ test {
70}70}
7171
72test {72test {
73 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));73 const bytes = rawIntBytes(u32, 0x4e5a7da9);
74 var fba = std.io.fixedBufferStream(&bytes);74 var fba = std.io.fixedBufferStream(&bytes);
75 const A = [2]u16;75 const A = [2]u16;
76 const a = try readType(fba.reader(), A, .Big);76 const a = try readType(fba.reader(), A, .Big);
...@@ -79,7 +79,7 @@ test {...@@ -79,7 +79,7 @@ test {
79}79}
8080
81test {81test {
82 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));82 const bytes = rawIntBytes(u32, 0x4e5a7da9);
83 var fba = std.io.fixedBufferStream(&bytes);83 var fba = std.io.fixedBufferStream(&bytes);
84 const S = packed struct {84 const S = packed struct {
85 a: u16,85 a: u16,
src/to_hex.zig+2-2
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;4const rawIntBytes = extras.rawIntBytes;
55
6pub fn to_hex(array: anytype) [array.len * 2]u8 {6pub fn to_hex(array: anytype) [array.len * 2]u8 {
7 var res: [array.len * 2]u8 = undefined;7 var res: [array.len * 2]u8 = undefined;
...@@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {...@@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {
11}11}
1212
13test {13test {
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"));
15}15}