tests
This commit is contained in:
parent
55f202ca9b
commit
eba788a926
|
@ -35,18 +35,22 @@ pub fn get_bytearray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayL
|
|||
pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
||||
var data = std.ArrayList(u8).init(allocator);
|
||||
defer data.deinit();
|
||||
|
||||
var length = try reader.readIntBig(i32);
|
||||
var chars = @divTrunc(@divTrunc(length, 8), 2);
|
||||
// std.debug.print("get_lenchars: {d} {d} \n", .{length, chars});
|
||||
|
||||
|
||||
var index: usize = 0;
|
||||
while (true) {
|
||||
if (index == length) break;
|
||||
if (index == chars) break;
|
||||
|
||||
const byte = try reader.readIntBig(u16);
|
||||
try data.append(@intCast(u8, byte));
|
||||
index += 1;
|
||||
}
|
||||
|
||||
var ut8Str = try allocator.alloc(u8, @intCast(usize, length));
|
||||
var ut8Str = try allocator.alloc(u8, @intCast(usize, chars));
|
||||
for (data.items) |char, i| {
|
||||
ut8Str[i] = char;
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ test "read/write string" {
|
|||
var val = try read.get_string(fBS.reader(), global_allocator);
|
||||
defer global_allocator.free(val);
|
||||
|
||||
try expect(std.mem.eql(u8, val, "Hello World!"));
|
||||
try std.testing.expectEqualStrings("Hello World!", val);
|
||||
}
|
||||
|
||||
test "read/write bytearray" {
|
||||
|
@ -78,7 +78,7 @@ test "read/write bytearray" {
|
|||
}
|
||||
}
|
||||
|
||||
test "read/write stringlist" {
|
||||
test "read/write strlist" {
|
||||
var byteList = std.ArrayList(u8).init(global_allocator);
|
||||
defer byteList.deinit();
|
||||
|
||||
|
|
|
@ -19,13 +19,14 @@ pub fn add_bytearray(writer: anytype, array: std.ArrayList(u8)) !void {
|
|||
}
|
||||
|
||||
pub fn add_string(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void {
|
||||
try writer.writeIntBig(i32, @intCast(i32, str.len*16));
|
||||
var ut16Str = try allocator.alloc(u16, str.len);
|
||||
defer allocator.free(ut16Str);
|
||||
for (str) |character, index| {
|
||||
ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character));
|
||||
}
|
||||
// std.debug.print("add_lenchars: {d} {d} \n", .{@intCast(i32, ut16Str.len*8*2), str.len});
|
||||
|
||||
try writer.writeIntBig(i32, @intCast(i32, ut16Str.len*8*2));
|
||||
try writer.writeAll(std.mem.sliceAsBytes(ut16Str));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue