test(C1.1): cSourceCacheKey — content key for compiled #source units

Source bytes, declared-header CONTENT (header edits invalidate),
defines/flags/include dirs in order, LLVM version, and target
triple/sysroot all participate; section tags keep equal strings in
different roles distinct. Pure function + variance property tests;
nothing consumes it yet.
This commit is contained in:
agra
2026-06-12 16:43:42 +03:00
parent 1009181638
commit 10f5a4318d
3 changed files with 113 additions and 0 deletions

68
src/c_import.test.zig Normal file
View File

@@ -0,0 +1,68 @@
const std = @import("std");
const c_import = @import("c_import.zig");
const SRC = "int f(void) { return 1; }";
const HDR = "int f(void);";
const VER = "19.1.7";
const none: []const []const u8 = &.{};
fn baseKey() u64 {
return c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
}
test "cSourceCacheKey: stable when nothing changes" {
try std.testing.expectEqual(baseKey(), baseKey());
}
test "cSourceCacheKey: source bytes vary the key" {
const other = c_import.cSourceCacheKey("int f(void) { return 2; }", &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other);
}
test "cSourceCacheKey: declared header content varies the key" {
const other = c_import.cSourceCacheKey(SRC, &.{"int f(void); int g(void);"}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other);
}
test "cSourceCacheKey: defines vary the key (value and order)" {
const v2 = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=2"}, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != v2);
const ab = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{ "A=1", "B=1" }, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
const ba = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{ "B=1", "A=1" }, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(ab != ba);
}
test "cSourceCacheKey: flags vary the key" {
const other = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O3"}, &.{"inc"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other);
}
test "cSourceCacheKey: a define is not a flag (same string, different role)" {
const as_define = c_import.cSourceCacheKey(SRC, none, &.{"X"}, none, none, VER, null, null);
const as_flag = c_import.cSourceCacheKey(SRC, none, none, &.{"X"}, none, VER, null, null);
try std.testing.expect(as_define != as_flag);
}
test "cSourceCacheKey: include dirs vary the key" {
const other = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"other"}, VER, "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other);
}
test "cSourceCacheKey: llvm version varies the key" {
const other = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, "20.0.0", "arm64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other);
}
test "cSourceCacheKey: triple and sysroot vary the key; absent is not empty" {
const other_triple = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, VER, "x86_64-apple-darwin", "/sdk");
try std.testing.expect(baseKey() != other_triple);
const other_sysroot = c_import.cSourceCacheKey(SRC, &.{HDR}, &.{"A=1"}, &.{"-O2"}, &.{"inc"}, VER, "arm64-apple-darwin", "/ndk");
try std.testing.expect(baseKey() != other_sysroot);
const absent = c_import.cSourceCacheKey(SRC, none, none, none, none, VER, null, null);
const empty = c_import.cSourceCacheKey(SRC, none, none, none, none, VER, "", "");
try std.testing.expect(absent != empty);
}