Skip to content

Commit 2418fd6

Browse files
committed
Add test to histogram-array
1 parent bc9d3bb commit 2418fd6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

histogram-array/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
axiprotocol "github.com/ReconfigureIO/sdaccel/axi/protocol"
99
)
1010

11+
// calculate the bin for the histogram
12+
func CalculateIndex(sample uint32) uint16 {
13+
return uint16(sample) >> (16 - 9)
14+
}
15+
1116
// magic identifier for exporting
1217
func Top(
1318
inputData uintptr,
@@ -32,11 +37,9 @@ func Top(
3237
for ; length > 0; length-- {
3338
// First we'll pull of each sample from the channel
3439
sample := <-inputChan
35-
// calculate the bin for the histogram
36-
index := uint16(sample) >> (16 - 9)
3740

3841
// And increment the value in that bin
39-
histogram[uint(index)] += 1
42+
histogram[CalculateIndex(sample)] += 1
4043
}
4144

4245
data := make(chan uint32)

histogram-array/main_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
"testing/quick"
6+
)
7+
8+
func TestCalculateIndexDoesNotOutOfBounds(t *testing.T) {
9+
// Check that we never generate an index out of bounds
10+
f := func(x uint32) bool {
11+
index := CalculateIndex(x)
12+
return index < 512
13+
}
14+
if err := quick.Check(f, nil); err != nil {
15+
t.Error(err)
16+
}
17+
}

0 commit comments

Comments
 (0)