Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pkg/analysis/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func GenerateFuzzyHash(t *FunctionTopology) string {
}
brBucket := 0
if t.BranchCount > 0 {
// FIX: Differentiate between 0 branches (linear) and 1 branch (single if).
// Differentiate between 0 branches (linear) and 1 branch (single if).
// Log2(1) is 0, so we shift by 1 to make BR0 mean "no branches" and BR1 mean "1 branch".
brBucket = int(math.Log2(float64(t.BranchCount))) + 1
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/analysis/topology/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,38 @@ func TestFuzzyHashDistinction(t *testing.T) {
}
}`

srcBranched2 := `package main
func branched2(b bool, c bool) {
if b {
println("branch 1")
}
if c {
println("branch 2")
}
}`

fnLinear := testutil.CompileAndGetFunction(t, srcLinear, "linear")
fnBranched := testutil.CompileAndGetFunction(t, srcBranched, "branched")
fnBranched2 := testutil.CompileAndGetFunction(t, srcBranched2, "branched2")

topoLinear := topology.ExtractTopology(fnLinear)
topoBranched := topology.ExtractTopology(fnBranched)
topoBranched2 := topology.ExtractTopology(fnBranched2)

// Linear: 0 branches -> BR0
if !strings.Contains(topoLinear.FuzzyHash, "BR0") {
t.Errorf("Linear code hash should contain BR0, got %q", topoLinear.FuzzyHash)
}

// Single If: 1 branch -> BR1
if !strings.Contains(topoBranched.FuzzyHash, "BR1") {
t.Errorf("Branched code (1 if) hash should contain BR1, got %q", topoBranched.FuzzyHash)
}

// Two Ifs: 2 branches -> BR2
if !strings.Contains(topoBranched2.FuzzyHash, "BR2") {
t.Errorf("Branched code (2 ifs) hash should contain BR2, got %q", topoBranched2.FuzzyHash)
}

if topoLinear.FuzzyHash == topoBranched.FuzzyHash {
t.Errorf("Fuzzy Hash Collision: Linear and Branched code produced same hash %q", topoLinear.FuzzyHash)
Expand Down
Loading