Skip to content

Commit

Permalink
tests: [zstd] Unit tests for sequenceDecs.adjustOffset (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMula committed Mar 10, 2022
1 parent 000e3ec commit 28fb801
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions zstd/seqdec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package zstd

import (
"testing"
)

func TestSequenceDecsAdjustOffset(t *testing.T) {
type result struct {
offset int
prevOffset [3]int
}

tc := []struct {
offset int
litLen int
offsetB uint8
prevOffset [3]int

res result
}{{
offset: 444,
litLen: 0,
offsetB: 42,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 444,
prevOffset: [3]int{444, 111, 222},
},
}, {
offset: 0,
litLen: 1,
offsetB: 0,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 111,
prevOffset: [3]int{111, 222, 333},
},
}, {
offset: -1,
litLen: 0,
offsetB: 0,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 111,
prevOffset: [3]int{111, 222, 333},
},
}, {
offset: 1,
litLen: 1,
offsetB: 0,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 222,
prevOffset: [3]int{222, 111, 333},
},
}, {
offset: 2,
litLen: 1,
offsetB: 0,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 333,
prevOffset: [3]int{333, 111, 222},
},
}, {
offset: 3,
litLen: 1,
offsetB: 0,
prevOffset: [3]int{111, 222, 333},

res: result{
offset: 110, // s.prevOffset[0] - 1
prevOffset: [3]int{110, 111, 222},
},
}, {
offset: 3,
litLen: 1,
offsetB: 0,
prevOffset: [3]int{1, 222, 333},

res: result{
offset: 1,
prevOffset: [3]int{1, 1, 222},
},
},
}

for i := range tc {
// given
var sd sequenceDecs
for j := 0; j < 3; j++ {
sd.prevOffset[j] = tc[i].prevOffset[j]
}

// when
offset := sd.adjustOffset(tc[i].offset, tc[i].litLen, tc[i].offsetB)

// then
if offset != tc[i].res.offset {
t.Logf("result: %d", offset)
t.Logf("expected: %d", tc[i].res.offset)
t.Errorf("testcase #%d: wrong function result", i)
}

for j := 0; j < 3; j++ {
if sd.prevOffset[j] != tc[i].res.prevOffset[j] {
t.Logf("result: %v", sd.prevOffset)
t.Logf("expected: %v", tc[i].res.prevOffset)
t.Errorf("testcase #%d: sd.prevOffset got wrongly updated", i)
break
}
}
}
}

0 comments on commit 28fb801

Please sign in to comment.