add unit tests for padding

This commit is contained in:
2022-02-19 16:04:13 +02:00
committed by Motiejus Jakštys
parent 623060dac3
commit ca28332126
3 changed files with 16 additions and 9 deletions

View File

@@ -170,7 +170,12 @@ pub const ShellWriter = struct {
// rounds up a u12 to the nearest factor of 4 and returns the difference
// (padding)
inline fn roundUp4Padding(n: u12) u12 {
return ((n + 3) & ~@intCast(u12, 3)) - n;
return roundUp4(n) - n;
}
// rounds up a u12 to the nearest factor of 4.
inline fn roundUp4(n: u12) u12 {
return ((n + 3) & ~@intCast(u12, 3));
}
// ShellIndex is an index to the shell strings. As shell can be up to 64 bytes
@@ -192,12 +197,12 @@ test "basic shellpopcon" {
const bash = "/bin/bash"; // 9 chars
const zsh = "/bin/zsh"; // 8 chars
const long = "/bin/very-long-shell-name-ought-to-be-first"; // 43 chars
const nobody = "/bin/nobody"; // only 1 instance, ought to ignore
const long = "/bin/very-long-shell-name-ought-to-be-first";
const input = [_][]const u8{
zsh, zsh, zsh, zsh, // zsh score 8*4=32
bash, bash, bash, nobody, // bash score 3*9=27
long, long, // long score 2*42=84
long, long, // long score 2*43=86
};
for (input) |shell| {
@@ -212,6 +217,10 @@ test "basic shellpopcon" {
try testing.expectEqual(sections.getIndex(zsh).?, 1);
try testing.expectEqual(sections.getIndex(bash).?, 2);
try testing.expectEqual(sections.getIndex(nobody), null);
try testing.expectEqual(
sections.sectionBlob().len,
roundUp4(bash.len) + roundUp4(zsh.len) + roundUp4(long.len),
);
const shellReader = ShellReader.init(
sections.sectionIndex(),
@@ -220,6 +229,8 @@ test "basic shellpopcon" {
try testing.expectEqualStrings(shellReader.get(0), long);
try testing.expectEqualStrings(shellReader.get(1), zsh);
try testing.expectEqualStrings(shellReader.get(2), bash);
try testing.expectEqual(shellReader.sectionIndex.len, 3);
}
test "padding" {