mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-23 08:48:03 +00:00
Make a test less hardcoded
This commit is contained in:
parent
e064d79e4f
commit
be700b1948
1 changed files with 29 additions and 65 deletions
|
|
@ -9,15 +9,22 @@ const Import = CIR.Import;
|
|||
const StringLiteral = base.StringLiteral;
|
||||
const CompactWriter = collections.CompactWriter;
|
||||
|
||||
fn storeContainsModule(store: *const Import.Store, string_store: *const StringLiteral.Store, module_name: []const u8) bool {
|
||||
for (store.imports.items.items) |string_idx| {
|
||||
if (std.mem.eql(u8, string_store.get(string_idx), module_name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
test "Import.Store deduplicates module names" {
|
||||
const testing = std.testing;
|
||||
const gpa = testing.allocator;
|
||||
|
||||
// Create a string store for interning module names
|
||||
var string_store = try StringLiteral.Store.initCapacityBytes(gpa, 1024);
|
||||
defer string_store.deinit(gpa);
|
||||
|
||||
// Create import store
|
||||
var store = Import.Store.init();
|
||||
defer store.deinit(gpa);
|
||||
|
||||
|
|
@ -25,7 +32,7 @@ test "Import.Store deduplicates module names" {
|
|||
const idx1 = try store.getOrPut(gpa, &string_store, "test.Module");
|
||||
const idx2 = try store.getOrPut(gpa, &string_store, "test.Module");
|
||||
|
||||
// Should get the same index
|
||||
// Should get the same index back (deduplication)
|
||||
try testing.expectEqual(idx1, idx2);
|
||||
try testing.expectEqual(@as(usize, 1), store.imports.len());
|
||||
|
||||
|
|
@ -39,21 +46,17 @@ test "Import.Store deduplicates module names" {
|
|||
try testing.expectEqual(idx1, idx4);
|
||||
try testing.expectEqual(@as(usize, 2), store.imports.len());
|
||||
|
||||
// Verify we can retrieve the module names through the string store
|
||||
const str_idx1 = store.imports.items.items[@intFromEnum(idx1)];
|
||||
const str_idx3 = store.imports.items.items[@intFromEnum(idx3)];
|
||||
try testing.expectEqualStrings("test.Module", string_store.get(str_idx1));
|
||||
try testing.expectEqualStrings("other.Module", string_store.get(str_idx3));
|
||||
// Verify both module names are present
|
||||
try testing.expect(storeContainsModule(&store, &string_store, "test.Module"));
|
||||
try testing.expect(storeContainsModule(&store, &string_store, "other.Module"));
|
||||
}
|
||||
|
||||
test "Import.Store empty CompactWriter roundtrip" {
|
||||
const testing = std.testing;
|
||||
const gpa = testing.allocator;
|
||||
|
||||
// Create an empty Store
|
||||
var original = Import.Store.init();
|
||||
|
||||
// Create a temp file
|
||||
var tmp_dir = testing.tmpDir(.{});
|
||||
defer tmp_dir.cleanup();
|
||||
|
||||
|
|
@ -66,15 +69,12 @@ test "Import.Store empty CompactWriter roundtrip" {
|
|||
const serialized = try writer.appendAlloc(gpa, Import.Store.Serialized);
|
||||
try serialized.serialize(&original, gpa, &writer);
|
||||
|
||||
// Write to file
|
||||
try writer.writeGather(gpa, file);
|
||||
|
||||
// Read back
|
||||
try file.seekTo(0);
|
||||
const buffer = try file.readToEndAlloc(gpa, 1024 * 1024);
|
||||
defer gpa.free(buffer);
|
||||
|
||||
// Cast to Serialized and deserialize
|
||||
const serialized_ptr = @as(*Import.Store.Serialized, @ptrCast(@alignCast(buffer.ptr)));
|
||||
const deserialized = try serialized_ptr.deserialize(@as(i64, @intCast(@intFromPtr(buffer.ptr))), gpa);
|
||||
|
||||
|
|
@ -87,29 +87,18 @@ test "Import.Store basic CompactWriter roundtrip" {
|
|||
const testing = std.testing;
|
||||
const gpa = testing.allocator;
|
||||
|
||||
// Create a mock module env with string store
|
||||
var string_store = try StringLiteral.Store.initCapacityBytes(gpa, 1024);
|
||||
defer string_store.deinit(gpa);
|
||||
|
||||
const MockEnv = struct { strings: *StringLiteral.Store };
|
||||
const mock_env = MockEnv{ .strings = &string_store };
|
||||
|
||||
// Create original store and add some imports
|
||||
var original = Import.Store.init();
|
||||
defer original.deinit(gpa);
|
||||
|
||||
const idx1 = try original.getOrPut(gpa, mock_env.strings, "json.Json");
|
||||
const idx2 = try original.getOrPut(gpa, mock_env.strings, "core.List");
|
||||
const idx3 = try original.getOrPut(gpa, mock_env.strings, "my.Module");
|
||||
_ = try original.getOrPut(gpa, &string_store, "json.Json");
|
||||
_ = try original.getOrPut(gpa, &string_store, "core.List");
|
||||
_ = try original.getOrPut(gpa, &string_store, "my.Module");
|
||||
|
||||
// Verify indices are distinct and in order
|
||||
try testing.expect(idx1 != idx2);
|
||||
try testing.expect(idx2 != idx3);
|
||||
try testing.expect(idx1 != idx3);
|
||||
try testing.expect(@intFromEnum(idx1) < @intFromEnum(idx2));
|
||||
try testing.expect(@intFromEnum(idx2) < @intFromEnum(idx3));
|
||||
try testing.expectEqual(@as(usize, 3), original.imports.len());
|
||||
|
||||
// Create a temp file
|
||||
var tmp_dir = testing.tmpDir(.{});
|
||||
defer tmp_dir.cleanup();
|
||||
|
||||
|
|
@ -122,30 +111,23 @@ test "Import.Store basic CompactWriter roundtrip" {
|
|||
const serialized = try writer.appendAlloc(gpa, Import.Store.Serialized);
|
||||
try serialized.serialize(&original, gpa, &writer);
|
||||
|
||||
// Write to file
|
||||
try writer.writeGather(gpa, file);
|
||||
|
||||
// Read back
|
||||
try file.seekTo(0);
|
||||
const buffer = try file.readToEndAlloc(gpa, 1024 * 1024);
|
||||
defer gpa.free(buffer);
|
||||
|
||||
// Cast to Serialized and deserialize
|
||||
const serialized_ptr: *Import.Store.Serialized = @ptrCast(@alignCast(buffer.ptr));
|
||||
var deserialized = try serialized_ptr.deserialize(@as(i64, @intCast(@intFromPtr(buffer.ptr))), gpa);
|
||||
defer deserialized.map.deinit(gpa);
|
||||
|
||||
// Verify the imports are accessible
|
||||
// Verify the correct number of imports
|
||||
try testing.expectEqual(@as(usize, 3), deserialized.imports.len());
|
||||
|
||||
// Verify the interned string IDs are stored correctly by using the indices we got
|
||||
const str_idx1 = deserialized.imports.items.items[@intFromEnum(idx1)];
|
||||
const str_idx2 = deserialized.imports.items.items[@intFromEnum(idx2)];
|
||||
const str_idx3 = deserialized.imports.items.items[@intFromEnum(idx3)];
|
||||
|
||||
try testing.expectEqualStrings("json.Json", string_store.get(str_idx1));
|
||||
try testing.expectEqualStrings("core.List", string_store.get(str_idx2));
|
||||
try testing.expectEqualStrings("my.Module", string_store.get(str_idx3));
|
||||
// Verify all expected module names are present by iterating
|
||||
try testing.expect(storeContainsModule(deserialized, &string_store, "json.Json"));
|
||||
try testing.expect(storeContainsModule(deserialized, &string_store, "core.List"));
|
||||
try testing.expect(storeContainsModule(deserialized, &string_store, "my.Module"));
|
||||
|
||||
// Verify the map is repopulated correctly
|
||||
try testing.expectEqual(@as(usize, 3), deserialized.map.count());
|
||||
|
|
@ -155,26 +137,20 @@ test "Import.Store duplicate imports CompactWriter roundtrip" {
|
|||
const testing = std.testing;
|
||||
const gpa = testing.allocator;
|
||||
|
||||
// Create a mock module env with string store
|
||||
var string_store = try StringLiteral.Store.initCapacityBytes(gpa, 1024);
|
||||
defer string_store.deinit(gpa);
|
||||
|
||||
const MockEnv = struct { strings: *StringLiteral.Store };
|
||||
const mock_env = MockEnv{ .strings = &string_store };
|
||||
|
||||
// Create store with duplicate imports
|
||||
var original = Import.Store.init();
|
||||
defer original.deinit(gpa);
|
||||
|
||||
const idx1 = try original.getOrPut(gpa, mock_env.strings, "test.Module");
|
||||
const idx2 = try original.getOrPut(gpa, mock_env.strings, "another.Module");
|
||||
const idx3 = try original.getOrPut(gpa, mock_env.strings, "test.Module"); // duplicate
|
||||
const idx1 = try original.getOrPut(gpa, &string_store, "test.Module");
|
||||
_ = try original.getOrPut(gpa, &string_store, "another.Module");
|
||||
const idx3 = try original.getOrPut(gpa, &string_store, "test.Module"); // duplicate
|
||||
|
||||
// Verify deduplication worked
|
||||
try testing.expectEqual(idx1, idx3);
|
||||
try testing.expectEqual(@as(usize, 2), original.imports.len());
|
||||
|
||||
// Create a temp file
|
||||
var tmp_dir = testing.tmpDir(.{});
|
||||
defer tmp_dir.cleanup();
|
||||
|
||||
|
|
@ -187,35 +163,23 @@ test "Import.Store duplicate imports CompactWriter roundtrip" {
|
|||
const serialized = try writer.appendAlloc(gpa, Import.Store.Serialized);
|
||||
try serialized.serialize(&original, gpa, &writer);
|
||||
|
||||
// Write to file
|
||||
try writer.writeGather(gpa, file);
|
||||
|
||||
// Read back
|
||||
try file.seekTo(0);
|
||||
const buffer = try file.readToEndAlloc(gpa, 1024 * 1024);
|
||||
defer gpa.free(buffer);
|
||||
|
||||
// Cast to Serialized and deserialize
|
||||
const serialized_ptr: *Import.Store.Serialized = @ptrCast(@alignCast(buffer.ptr));
|
||||
var deserialized = try serialized_ptr.deserialize(@as(i64, @intCast(@intFromPtr(buffer.ptr))), gpa);
|
||||
defer deserialized.map.deinit(gpa);
|
||||
|
||||
// Verify correct number of imports
|
||||
// Verify correct number of imports (duplicates deduplicated)
|
||||
try testing.expectEqual(@as(usize, 2), deserialized.imports.len());
|
||||
|
||||
// Get the string IDs using the indices we captured and verify the strings
|
||||
const str_idx1 = deserialized.imports.items.items[@intFromEnum(idx1)];
|
||||
const str_idx2 = deserialized.imports.items.items[@intFromEnum(idx2)];
|
||||
|
||||
try testing.expectEqualStrings("test.Module", string_store.get(str_idx1));
|
||||
try testing.expectEqualStrings("another.Module", string_store.get(str_idx2));
|
||||
// Verify expected module names are present
|
||||
try testing.expect(storeContainsModule(deserialized, &string_store, "test.Module"));
|
||||
try testing.expect(storeContainsModule(deserialized, &string_store, "another.Module"));
|
||||
|
||||
// Verify the map was repopulated correctly
|
||||
try testing.expectEqual(@as(usize, 2), deserialized.map.count());
|
||||
|
||||
// Check that the map has correct entries for the string indices that were deserialized
|
||||
try testing.expect(deserialized.map.contains(str_idx1));
|
||||
try testing.expect(deserialized.map.contains(str_idx2));
|
||||
try testing.expectEqual(idx1, deserialized.map.get(str_idx1).?);
|
||||
try testing.expectEqual(idx2, deserialized.map.get(str_idx2).?);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue