Skip to content

Commit aeeb886

Browse files
committed
test(database): add iterator nextKey and nextValue sanity check test
1 parent b470156 commit aeeb886

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/ledger/database/interface.zig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,5 +765,41 @@ pub fn testDatabase(comptime Impl: fn ([]const ColumnFamily) type) type {
765765
try std.testing.expectEqual(null, try db.get(allocator, cf1, 30));
766766
try std.testing.expect(null != try db.get(allocator, cf1, 40));
767767
}
768+
769+
test "iterator nextKey and nextValue" {
770+
// Since keys use big-endian and values use little-endian serialization,
771+
// this would produce incorrect results when the key and value types differ.
772+
const allocator = std.testing.allocator;
773+
const path = test_dir ++ @src().fn_name;
774+
try ledger.tests.freshDir(path);
775+
var db = try DB.open(allocator, .noop, path, false);
776+
defer db.deinit();
777+
778+
try db.put(cf1, 1, .{ .hello = 111 });
779+
try db.put(cf1, 2, .{ .hello = 222 });
780+
try db.put(cf1, 3, .{ .hello = 333 });
781+
782+
// Test nextKey
783+
{
784+
var iter = try db.iterator(cf1, .forward, null);
785+
defer iter.deinit();
786+
787+
try std.testing.expectEqual(@as(u64, 1), try iter.nextKey());
788+
try std.testing.expectEqual(@as(u64, 2), try iter.nextKey());
789+
try std.testing.expectEqual(@as(u64, 3), try iter.nextKey());
790+
try std.testing.expectEqual(null, try iter.nextKey());
791+
}
792+
793+
// Test nextValue
794+
{
795+
var iter = try db.iterator(cf1, .forward, null);
796+
defer iter.deinit();
797+
798+
try std.testing.expectEqual(Value1{ .hello = 111 }, try iter.nextValue());
799+
try std.testing.expectEqual(Value1{ .hello = 222 }, try iter.nextValue());
800+
try std.testing.expectEqual(Value1{ .hello = 333 }, try iter.nextValue());
801+
try std.testing.expectEqual(null, try iter.nextValue());
802+
}
803+
}
768804
};
769805
}

0 commit comments

Comments
 (0)