Skip to content

Commit d43f707

Browse files
committed
test(database): add iterator nextKey and nextValue sanity check test
1 parent 34b23f0 commit d43f707

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

0 commit comments

Comments
 (0)