Skip to content

Commit c99edc6

Browse files
authored
Merge pull request #12 from BlackVectorOps/fix-topology-branch-comments-11522845551089852395
Refine branch differentiation comment and add hash verification tests
2 parents 11fdbb8 + 6f2d241 commit c99edc6

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

pkg/analysis/topology/topology.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func GenerateFuzzyHash(t *FunctionTopology) string {
277277
}
278278
brBucket := 0
279279
if t.BranchCount > 0 {
280-
// FIX: Differentiate between 0 branches (linear) and 1 branch (single if).
280+
// Differentiate between 0 branches (linear) and 1 branch (single if).
281281
// Log2(1) is 0, so we shift by 1 to make BR0 mean "no branches" and BR1 mean "1 branch".
282282
brBucket = int(math.Log2(float64(t.BranchCount))) + 1
283283
}

pkg/analysis/topology/topology_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,38 @@ func TestFuzzyHashDistinction(t *testing.T) {
188188
}
189189
}`
190190

191+
srcBranched2 := `package main
192+
func branched2(b bool, c bool) {
193+
if b {
194+
println("branch 1")
195+
}
196+
if c {
197+
println("branch 2")
198+
}
199+
}`
200+
191201
fnLinear := testutil.CompileAndGetFunction(t, srcLinear, "linear")
192202
fnBranched := testutil.CompileAndGetFunction(t, srcBranched, "branched")
203+
fnBranched2 := testutil.CompileAndGetFunction(t, srcBranched2, "branched2")
193204

194205
topoLinear := topology.ExtractTopology(fnLinear)
195206
topoBranched := topology.ExtractTopology(fnBranched)
207+
topoBranched2 := topology.ExtractTopology(fnBranched2)
208+
209+
// Linear: 0 branches -> BR0
210+
if !strings.Contains(topoLinear.FuzzyHash, "BR0") {
211+
t.Errorf("Linear code hash should contain BR0, got %q", topoLinear.FuzzyHash)
212+
}
213+
214+
// Single If: 1 branch -> BR1
215+
if !strings.Contains(topoBranched.FuzzyHash, "BR1") {
216+
t.Errorf("Branched code (1 if) hash should contain BR1, got %q", topoBranched.FuzzyHash)
217+
}
218+
219+
// Two Ifs: 2 branches -> BR2
220+
if !strings.Contains(topoBranched2.FuzzyHash, "BR2") {
221+
t.Errorf("Branched code (2 ifs) hash should contain BR2, got %q", topoBranched2.FuzzyHash)
222+
}
196223

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

0 commit comments

Comments
 (0)