authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-16 16:11:39+00:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-06-16 16:20:59+00:00
log38e69a9e6a6d69a48d130382dcb61ba2516863d0
tree37283197df2b75d0edbee1ed8e112efb42442837
parenta0160d776f30fab70c786cd1159203a593af51b4

Added test to ensure minimum number of bytes is emitted for writes


1 files changed, 33 insertions(+), 22 deletions(-)

lib/std/debug/leb128.zig+33-22
......@@ -1,7 +1,7 @@
11const std = @import("std");
22const testing = std.testing;
33
4///Read a single unsigned LEB128 value from the given reader as type T,
4/// Read a single unsigned LEB128 value from the given reader as type T,
55/// or error.Overflow if the value cannot fit.
66pub fn readULEB128(comptime T: type, reader: var) !T {
77 const U = if (T.bit_count < 8) u8 else T;
......@@ -24,7 +24,7 @@ pub fn readULEB128(comptime T: type, reader: var) !T {
2424 return error.Overflow;
2525 }
2626
27 //only applies in the case that we extended to u8
27 // only applies in the case that we extended to u8
2828 if (U != T) {
2929 if (value > std.math.maxInt(T)) return error.Overflow;
3030 }
......@@ -32,7 +32,7 @@ pub fn readULEB128(comptime T: type, reader: var) !T {
3232 return @truncate(T, value);
3333}
3434
35///Write a single unsigned integer as unsigned LEB128 to the given writer.
35/// Write a single unsigned integer as unsigned LEB128 to the given writer.
3636pub fn writeULEB128(writer: var, uint_value: var) !void {
3737 const T = @TypeOf(uint_value);
3838 const U = if (T.bit_count < 8) u8 else T;
......@@ -50,7 +50,7 @@ pub fn writeULEB128(writer: var, uint_value: var) !void {
5050 }
5151}
5252
53///Read a single unsinged integer from the given memory as type T.
53/// Read a single unsinged integer from the given memory as type T.
5454/// The provided slice reference will be updated to point to the byte after the last byte read.
5555pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T {
5656 var buf = std.io.fixedBufferStream(ptr.*);
......@@ -59,7 +59,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T {
5959 return value;
6060}
6161
62///Write a single unsigned LEB128 integer to the given memory as unsigned LEB128,
62/// Write a single unsigned LEB128 integer to the given memory as unsigned LEB128,
6363/// returning the number of bytes written.
6464pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {
6565 const T = @TypeOf(uint_value);
......@@ -69,7 +69,7 @@ pub fn writeULEB128Mem(ptr: []u8, uint_value: var) !usize {
6969 return buf.pos;
7070}
7171
72///Read a single signed LEB128 value from the given reader as type T,
72/// Read a single signed LEB128 value from the given reader as type T,
7373/// or error.Overflow if the value cannot fit.
7474pub fn readILEB128(comptime T: type, reader: var) !T {
7575 const S = if (T.bit_count < 8) i8 else T;
......@@ -87,11 +87,11 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
8787
8888 const shift = group * 7;
8989 if (@shlWithOverflow(U, temp, shift, &temp)) {
90 //Overflow is ok so long as the sign bit is set and this is the last byte
90 // Overflow is ok so long as the sign bit is set and this is the last byte
9191 if (byte & 0x80 != 0) return error.Overflow;
9292 if (@bitCast(S, temp) >= 0) return error.Overflow;
9393
94 //and all the overflowed bits are 1
94 // and all the overflowed bits are 1
9595 const remaining_shift = @intCast(u3, U.bit_count - @as(u16, shift));
9696 const remaining_bits = @bitCast(i8, byte | 0x80) >> remaining_shift;
9797 if (remaining_bits != -1) return error.Overflow;
......@@ -111,7 +111,7 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
111111 }
112112
113113 const result = @bitCast(S, value);
114 //Only applies if we extended to i8
114 // Only applies if we extended to i8
115115 if (S != T) {
116116 if (result > std.math.maxInt(T) or result < std.math.minInt(T)) return error.Overflow;
117117 }
......@@ -119,7 +119,7 @@ pub fn readILEB128(comptime T: type, reader: var) !T {
119119 return @truncate(T, result);
120120}
121121
122///Write a single signed integer as signed LEB128 to the given writer.
122/// Write a single signed integer as signed LEB128 to the given writer.
123123pub fn writeILEB128(writer: var, int_value: var) !void {
124124 const T = @TypeOf(int_value);
125125 const S = if (T.bit_count < 8) i8 else T;
......@@ -141,7 +141,7 @@ pub fn writeILEB128(writer: var, int_value: var) !void {
141141 }
142142}
143143
144///Read a single singed LEB128 integer from the given memory as type T.
144/// Read a single singed LEB128 integer from the given memory as type T.
145145/// The provided slice reference will be updated to point to the byte after the last byte read.
146146pub fn readILEB128Mem(comptime T: type, ptr: *[]const u8) !T {
147147 var buf = std.io.fixedBufferStream(ptr.*);
......@@ -150,7 +150,7 @@ pub fn readILEB128Mem(comptime T: type, ptr: *[]const u8) !T {
150150 return value;
151151}
152152
153///Write a single signed LEB128 integer to the given memory as unsigned LEB128,
153/// Write a single signed LEB128 integer to the given memory as unsigned LEB128,
154154/// returning the number of bytes written.
155155pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {
156156 const T = @TypeOf(int_value);
......@@ -159,7 +159,7 @@ pub fn writeILEB128Mem(ptr: []u8, int_value: var) !usize {
159159 return buf.pos;
160160}
161161
162//tests
162// tests
163163fn test_read_stream_ileb128(comptime T: type, encoded: []const u8) !T {
164164 var reader = std.io.fixedBufferStream(encoded);
165165 return try readILEB128(T, reader.reader());
......@@ -303,43 +303,54 @@ fn test_write_leb128(value: var) !void {
303303 const readStream = if (T.is_signed) readILEB128 else readULEB128;
304304 const readMem = if (T.is_signed) readILEB128Mem else readULEB128Mem;
305305
306 //decode to a larger bit size too, to ensure sign extension
306 // decode to a larger bit size too, to ensure sign extension
307307 // is working as expected
308308 const larger_type_bits = ((T.bit_count + 8) / 8) * 8;
309309 const B = std.meta.Int(T.is_signed, larger_type_bits);
310
311 const bytes_needed = bn: {
312 const S = std.meta.Int(T.is_signed, @sizeOf(T) * 8);
313 if (T.bit_count <= 7) break :bn @as(u16, 1);
314
315 const unused_bits = if (value < 0) @clz(T, ~value) else @clz(T, value);
316 const used_bits: u16 = (T.bit_count - unused_bits) + @boolToInt(T.is_signed);
317 if (used_bits <= 7) break :bn @as(u16, 1);
318 break :bn ((used_bits + 6) / 7);
319 };
320
310321 const max_groups = if (T.bit_count == 0) 1 else (T.bit_count + 6) / 7;
311322
312323 var buf: [max_groups]u8 = undefined;
313324 var fbs = std.io.fixedBufferStream(&buf);
314325
315 //stream write
326 // stream write
316327 try writeStream(fbs.writer(), value);
317328 const w1_pos = fbs.pos;
318 testing.expect(w1_pos > 0);
329 testing.expect(w1_pos == bytes_needed);
319330
320 //stream read
331 // stream read
321332 fbs.pos = 0;
322333 const sr = try readStream(T, fbs.reader());
323334 testing.expect(fbs.pos == w1_pos);
324335 testing.expect(sr == value);
325336
326 //bigger type stream read
337 // bigger type stream read
327338 fbs.pos = 0;
328339 const bsr = try readStream(B, fbs.reader());
329340 testing.expect(fbs.pos == w1_pos);
330341 testing.expect(bsr == value);
331342
332 //mem write
343 // mem write
333344 const w2_pos = try writeMem(&buf, value);
334345 testing.expect(w2_pos == w1_pos);
335346
336 //mem read
347 // mem read
337348 var buf_ref: []u8 = buf[0..];
338349 const mr = try readMem(T, &buf_ref);
339350 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
340351 testing.expect(mr == value);
341352
342 //bigger type mem read
353 // bigger type mem read
343354 buf_ref = buf[0..];
344355 const bmr = try readMem(T, &buf_ref);
345356 testing.expect(@ptrToInt(buf_ref.ptr) - @ptrToInt(&buf) == w2_pos);
......@@ -361,7 +372,7 @@ test "serialize unsigned LEB128" {
361372}
362373
363374test "serialize signed LEB128" {
364 //explicitly test i0 because starting `t` at 0
375 // explicitly test i0 because starting `t` at 0
365376 // will break the while loop
366377 try test_write_leb128(@as(i0, 0));
367378