Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 0 additions & 200 deletions src/ssz/tree_view/array_basic_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,207 +154,7 @@ test "TreeView vector getAllAlloc repeat reflects updates" {
try std.testing.expectEqualSlices(u32, values[0..], second);
}

test "TreeView vector clone isolates subsequent updates" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const Uint16 = UintType(16);
const Vec4 = FixedVectorType(Uint16, 4, .{});

const value: Vec4.Type = [_]u16{ 0, 0, 0, 0 };
const root = try Vec4.tree.fromValue(&pool, &value);

var v1 = try Vec4.TreeView.init(allocator, &pool, root);
defer v1.deinit();

var v2 = try v1.clone(.{});
defer v2.deinit();

try v2.set(1, @as(u16, 9));
try v2.commit();

try std.testing.expectEqual(@as(u16, 0), try v1.get(1));
try std.testing.expectEqual(@as(u16, 9), try v2.get(1));
}

test "TreeView vector clone reads committed state" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const Uint16 = UintType(16);
const Vec4 = FixedVectorType(Uint16, 4, .{});

const value: Vec4.Type = [_]u16{ 0, 0, 0, 0 };
const root = try Vec4.tree.fromValue(&pool, &value);

var v1 = try Vec4.TreeView.init(allocator, &pool, root);
defer v1.deinit();

try v1.set(2, @as(u16, 7));
try v1.commit();

var v2 = try v1.clone(.{});
defer v2.deinit();

try std.testing.expectEqual(@as(u16, 7), try v2.get(2));
}

test "TreeView vector clone drops uncommitted changes" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const Uint16 = UintType(16);
const Vec4 = FixedVectorType(Uint16, 4, .{});

const value: Vec4.Type = [_]u16{ 1, 2, 3, 4 };
const root = try Vec4.tree.fromValue(&pool, &value);

var v = try Vec4.TreeView.init(allocator, &pool, root);
defer v.deinit();

try v.set(0, @as(u16, 9));
try std.testing.expectEqual(@as(u16, 9), try v.get(0));

var dropped = try v.clone(.{});
defer dropped.deinit();

try std.testing.expectEqual(@as(u16, 1), try v.get(0));
try std.testing.expectEqual(@as(u16, 1), try dropped.get(0));
}

test "TreeView vector clone(false) does not transfer cache" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const Uint16 = UintType(16);
const Vec4 = FixedVectorType(Uint16, 4, .{});

const value: Vec4.Type = [_]u16{ 1, 2, 3, 4 };
const root = try Vec4.tree.fromValue(&pool, &value);

var v = try Vec4.TreeView.init(allocator, &pool, root);
defer v.deinit();

_ = try v.get(0);
try std.testing.expect(v.chunks.state.children_nodes.count() > 0);

var cloned_no_cache = try v.clone(.{ .transfer_cache = false });
defer cloned_no_cache.deinit();

try std.testing.expect(v.chunks.state.children_nodes.count() > 0);
try std.testing.expectEqual(@as(usize, 0), cloned_no_cache.chunks.state.children_nodes.count());
}

test "TreeView vector clone(true) transfers cache and clears source" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const Uint16 = UintType(16);
const Vec4 = FixedVectorType(Uint16, 4, .{});

const value: Vec4.Type = [_]u16{ 1, 2, 3, 4 };
const root = try Vec4.tree.fromValue(&pool, &value);

var v = try Vec4.TreeView.init(allocator, &pool, root);
defer v.deinit();

_ = try v.get(0);
try std.testing.expect(v.chunks.state.children_nodes.count() > 0);

var cloned = try v.clone(.{});
defer cloned.deinit();

try std.testing.expectEqual(@as(usize, 0), v.chunks.state.children_nodes.count());
try std.testing.expect(cloned.chunks.state.children_nodes.count() > 0);
}

// Tests ported from TypeScript ssz packages/ssz/test/unit/byType/vector/tree.test.ts
test "ArrayBasicTreeView - serialize (uint64 vector)" {
const allocator = std.testing.allocator;

const Uint64 = UintType(64);
const VecU64Type = FixedVectorType(Uint64, 4, .{});

var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const TestCase = struct {
id: []const u8,
values: [4]u64,
expected_serialized: []const u8,
expected_root: [32]u8,
};

const test_cases = [_]TestCase{
.{
.id = "4 values",
.values = [4]u64{ 100000, 200000, 300000, 400000 },
// 0xa086010000000000400d030000000000e093040000000000801a060000000000
.expected_serialized = &[_]u8{ 0xa0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x93, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1a, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 },
// For VectorBasic, the root is the same as the serialized bytes (fits in one chunk)
.expected_root = [_]u8{ 0xa0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x93, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1a, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 },
},
};

for (test_cases) |tc| {
const value = tc.values;

var value_serialized: [VecU64Type.fixed_size]u8 = undefined;
_ = VecU64Type.serializeIntoBytes(&value, &value_serialized);

const tree_node = try VecU64Type.tree.fromValue(&pool, &value);
var view = try VecU64Type.TreeView.init(allocator, &pool, tree_node);
defer view.deinit();

var view_serialized: [VecU64Type.fixed_size]u8 = undefined;
const written = try view.serializeIntoBytes(&view_serialized);
try std.testing.expectEqual(view_serialized.len, written);

try std.testing.expectEqualSlices(u8, tc.expected_serialized, &view_serialized);
try std.testing.expectEqualSlices(u8, &value_serialized, &view_serialized);

const view_size = view.serializedSize();
try std.testing.expectEqual(tc.expected_serialized.len, view_size);

var hash_root: [32]u8 = undefined;
try view.hashTreeRootInto(&hash_root);
try std.testing.expectEqualSlices(u8, &tc.expected_root, &hash_root);
}
}

test "ArrayBasicTreeView - serialize (uint8 vector)" {
const allocator = std.testing.allocator;

const Uint8 = UintType(8);
const VecU8Type = FixedVectorType(Uint8, 8, .{});

var pool = try Node.Pool.init(.{ .page_allocator = allocator, .allocator = allocator, .pool_size = 1024 });
defer pool.deinit();

const value = [8]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };

var value_serialized: [VecU8Type.fixed_size]u8 = undefined;
_ = VecU8Type.serializeIntoBytes(&value, &value_serialized);

const tree_node = try VecU8Type.tree.fromValue(&pool, &value);
var view = try VecU8Type.TreeView.init(allocator, &pool, tree_node);
defer view.deinit();

var view_serialized: [VecU8Type.fixed_size]u8 = undefined;
const written = try view.serializeIntoBytes(&view_serialized);
try std.testing.expectEqual(view_serialized.len, written);

try std.testing.expectEqualSlices(u8, &value, &view_serialized);

const view_size = view.serializedSize();
try std.testing.expectEqual(@as(usize, 8), view_size);
}

test "ArrayBasicTreeView - get and set" {
const allocator = std.testing.allocator;

Expand Down
Loading
Loading