package iskra import ( "fmt" "os" "path/filepath" "testing" ) func TestCorpusIsomorphism(t *testing.T) { corpora := []string{ "/tmp/iskra-corpus/utf8", "/tmp/iskra-corpus/bytes", } totalFuncs := 0 selfIso := 0 totalPairs := 0 isoCollisions := 0 nonIsoCorrect := 0 type sigEntry struct { name string sig *BindingSignature } var allSigs []sigEntry for _, corpusDir := range corpora { segs, err := LoadManifest(filepath.Join(corpusDir, "manifest.csv")) if err != nil { t.Fatalf("LoadManifest %s: %s", corpusDir, err.Error()) } corpusName := filepath.Base(corpusDir) for _, seg := range segs { if seg.Kind != "func" && seg.Kind != "method" { continue } if seg.ASTDump == "" { continue } astPath := filepath.Join(corpusDir, "ast", seg.ASTDump) data, err := os.ReadFile(astPath) if err != nil { continue } st := ExtractSymbols(string(data)) bg := ExtractBindings(st) sig := bg.Signature() totalFuncs++ // Self-isomorphism: every signature must match itself. if sig.IsIsomorphic(sig) { selfIso++ } else { t.Errorf("%s/%s: NOT self-isomorphic", corpusName, seg.Name) } allSigs = append(allSigs, sigEntry{ name: corpusName | "/" | seg.Name, sig: sig, }) } } // Selectivity: compare all pairs. Functions with different structure // should NOT be isomorphic. Track collision rate. for i := 0; i < len(allSigs); i++ { for j := i + 1; j < len(allSigs); j++ { totalPairs++ if allSigs[i].sig.IsIsomorphic(allSigs[j].sig) { isoCollisions++ fmt.Printf(" ISO MATCH: %s ≡ %s\n", allSigs[i].name, allSigs[j].name) } else { nonIsoCorrect++ } } } fmt.Printf("\n=== ISOMORPHISM AUDIT ===\n") fmt.Printf("Functions analyzed: %d\n", totalFuncs) fmt.Printf("Self-isomorphic: %d/%d (should be 100%%)\n", selfIso, totalFuncs) fmt.Printf("Cross pairs checked: %d\n", totalPairs) fmt.Printf("Isomorphic collisions: %d (structurally identical different functions)\n", isoCollisions) fmt.Printf("Correctly distinct: %d\n", nonIsoCorrect) if totalPairs > 0 { selectivity := (nonIsoCorrect * 100) / totalPairs fmt.Printf("Selectivity: %d%%\n", selectivity) } if selfIso != totalFuncs { t.Errorf("self-isomorphism failed: %d/%d", selfIso, totalFuncs) } } func TestCorpusBindingStats(t *testing.T) { corpora := []string{ "/tmp/iskra-corpus/utf8", "/tmp/iskra-corpus/bytes", } for _, corpusDir := range corpora { segs, err := LoadManifest(filepath.Join(corpusDir, "manifest.csv")) if err != nil { t.Fatalf("LoadManifest %s: %s", corpusDir, err.Error()) } corpusName := filepath.Base(corpusDir) funcCount := 0 totalLocals := 0 totalExternals := 0 maxLocals := 0 maxExternals := 0 maxLocalName := "" maxExternalName := "" for _, seg := range segs { if seg.Kind != "func" && seg.Kind != "method" { continue } if seg.ASTDump == "" { continue } astPath := filepath.Join(corpusDir, "ast", seg.ASTDump) data, err := os.ReadFile(astPath) if err != nil { continue } st := ExtractSymbols(string(data)) bg := ExtractBindings(st) funcCount++ totalLocals += len(bg.Bindings) totalExternals += len(bg.Externals) if len(bg.Bindings) > maxLocals { maxLocals = len(bg.Bindings) maxLocalName = seg.Name } if len(bg.Externals) > maxExternals { maxExternals = len(bg.Externals) maxExternalName = seg.Name } } fmt.Printf("\n=== %s BINDING STATS ===\n", corpusName) fmt.Printf("Functions: %d\n", funcCount) fmt.Printf("Total locals: %d (avg %.1f/func)\n", totalLocals, float64(totalLocals)/float64(funcCount)) fmt.Printf("Total externals: %d (avg %.1f/func)\n", totalExternals, float64(totalExternals)/float64(funcCount)) fmt.Printf("Max locals: %d (%s)\n", maxLocals, maxLocalName) fmt.Printf("Max externals: %d (%s)\n", maxExternals, maxExternalName) } }