add usergids

This commit is contained in:
2022-03-04 10:37:07 +02:00
committed by Motiejus Jakštys
parent a4e3e08f5f
commit b81072f726
4 changed files with 119 additions and 35 deletions

View File

@@ -5,6 +5,9 @@
// golang's varint implementation.
const std = @import("std");
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
// compresses a strictly incrementing sorted slice of integers using delta
// compression. Compression is in-place.
pub fn deltaCompress(comptime T: type, elems: []T) error{NotSorted}!void {
@@ -48,7 +51,7 @@ test "delta compress/decompress" {
.{ .input = &[_]u8{ 0, 254, 255 }, .want = &[_]u8{ 0, 253, 0 } },
};
for (tests) |t| {
var arr = try std.ArrayList(u8).initCapacity(
var arr = try ArrayList(u8).initCapacity(
testing.allocator,
t.input.len,
);
@@ -69,7 +72,7 @@ test "delta compression negative tests" {
&[_]u8{ 0, 1, 1 },
&[_]u8{ 0, 1, 2, 1 },
}) |t| {
var arr = try std.ArrayList(u8).initCapacity(testing.allocator, t.len);
var arr = try ArrayList(u8).initCapacity(testing.allocator, t.len);
defer arr.deinit();
try arr.appendSlice(t);
try testing.expectError(error.NotSorted, deltaCompress(u8, arr.items));
@@ -81,7 +84,7 @@ test "delta decompress overflow" {
&[_]u8{ 255, 0 },
&[_]u8{ 0, 128, 127 },
}) |t| {
var arr = try std.ArrayList(u8).initCapacity(testing.allocator, t.len);
var arr = try ArrayList(u8).initCapacity(testing.allocator, t.len);
defer arr.deinit();
try arr.appendSlice(t);
try testing.expectError(error.Overflow, deltaDecompress(u8, arr.items));
@@ -142,24 +145,31 @@ pub fn putUvarint(buf: []u8, x: u64) usize {
return i + 1;
}
test "uvarint" {
const uvarint_tests = [_]u64{
0,
1,
2,
10,
20,
63,
64,
65,
127,
128,
129,
255,
256,
257,
1 << 63 - 1,
};
pub fn appendUvarint(arr: *ArrayList(u8), x: u64) Allocator.Error!void {
var buf: [maxVarintLen64]u8 = undefined;
const n = putUvarint(&buf, x);
try arr.appendSlice(buf[0..n]);
}
const uvarint_tests = [_]u64{
0,
1,
2,
10,
20,
63,
64,
65,
127,
128,
129,
255,
256,
257,
1 << 63 - 1,
};
test "putUvarint/uvarint" {
for (uvarint_tests) |x| {
var buf: [maxVarintLen64]u8 = undefined;
const n = putUvarint(buf[0..], x);
@@ -170,6 +180,18 @@ test "uvarint" {
}
}
test "appendUvarint" {
for (uvarint_tests) |x| {
var buf = ArrayList(u8).init(testing.allocator);
defer buf.deinit();
try appendUvarint(&buf, x);
const got = try uvarint(buf.items);
try testing.expectEqual(x, got.value);
}
}
test "overflow" {
for ([_][]const u8{
&[_]u8{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2 },