Commit f6780aaa authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 8bbd4088
...@@ -22,11 +22,104 @@ package blib ...@@ -22,11 +22,104 @@ package blib
//go:generate ./gen-rangemap _RangedMap_str string zrangemap_str.go //go:generate ./gen-rangemap _RangedMap_str string zrangemap_str.go
import ( import (
"reflect"
"testing" "testing"
) )
func TestRangeMap(t *testing.T) { func TestRangedMap(t *testing.T) {
type RangedMap = _RangedMap_str
type RangedMapEntry = _RangedMap_strEntry
type testEntry struct {
A *RangedMap
B KeyRange
Set *RangedMap // A.SetRange(B, x)
Del *RangedMap // A.DelRange(B)
Has bool // A.HasRange(B)
// XXX Get?
}
E := func(A *RangedMap, B KeyRange, S, D *RangedMap, H bool) testEntry {
return testEntry{A, B, S, D, H}
}
// M is shorthand to create RangedMap, e.g. M(1,2,"a", 3,4,"b") will return {[1,2):"a" [3,4):"b"}.
M := func(argv ...interface{}) *RangedMap {
l := len(argv)
if l % 3 != 0 {
panic("non 3x number of arguments")
}
// askey converts arg to Key
asKey := func(arg interface{}) Key {
// go through reflect to accept all int, int64, Key, ...
return Key(reflect.ValueOf(arg).Int())
}
M := &RangedMap{}
for i := 0; i < l/3; i++ {
// construct .entryv directly, not via SetRange
lo := asKey(argv[3*i])
hi := asKey(argv[3*i+1])
v := argv[3*i+2].(string)
hi_ := hi
if hi_ != oo {
hi_--
}
M.entryv = append(M.entryv, RangedMapEntry{
v,
KeyRange{lo, hi_},
})
}
M.verify()
return M
}
// K is alias for KeyRange
type K = KeyRange
// y, n alias true/false
const y, n = true, false
// a, b, c, d ... are shorthands for corresponding strings
const a, b, c, d, x = "a", "b", "c", "d", "x"
// XXX SetRange: // XXX SetRange:
// - coalesce (value same) // - coalesce (value same)
// - shrink left/right (value !same) + new entry // - shrink left/right (value !same) + new entry
testv := []testEntry{
E(
M(1,2,a, 2,3,b), // A
K{1,3}, // B
M(1,3,x), // Set
M(), // Del
y), // Has
}
for _, tt := range testv {
A := tt.A
B := tt.B
has := A.HasRange(B)
Aset := A.Clone()
Adel := A.Clone()
Aset.SetRange(B, x)
Adel.DelRange(B)
if !(has == tt.Has) {
t.Errorf("HasRange:\n A: %s\n B: %s\n ->·: %t\n ok·: %t\n", A, B, has, tt.Has)
}
if !Aset.Equal(tt.Set) {
t.Errorf("SetRange:\n A: %s\n B: %s\n ->·: %s\n ok·: %s\n", A, RangedMapEntry{x,B}, Aset, tt.Set)
}
if !Adel.Equal(tt.Del) {
t.Errorf("DelRange:\n A: %s\n B: %s\n ->·: %s\n ok·: %s\n", A, B, Adel, tt.Del)
}
}
} }
...@@ -31,7 +31,7 @@ func TestRangedKeySetTypes(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestRangedKeySetTypes(t *testing.T) {
t.Errorf("sizeof(void) = %d ; want 0", sizeVoid) t.Errorf("sizeof(void) = %d ; want 0", sizeVoid)
} }
// verify sizeof(set-entry) = sizeof(KeyRange) // verify that sizeof(set-entry) = sizeof(KeyRange)
// NOTE for this to be true Value needs to come first in RangedMapEntry // NOTE for this to be true Value needs to come first in RangedMapEntry
// (see github.com/golang/go/issues/48651) // (see github.com/golang/go/issues/48651)
sizeKeyRange := unsafe.Sizeof(KeyRange{}) sizeKeyRange := unsafe.Sizeof(KeyRange{})
...@@ -42,12 +42,12 @@ func TestRangedKeySetTypes(t *testing.T) { ...@@ -42,12 +42,12 @@ func TestRangedKeySetTypes(t *testing.T) {
} }
} }
func TestRangedKeySet(t *testing.T) { const (
const ( oo = KeyMax
oo = KeyMax noo = KeyMin
noo = KeyMin )
)
func TestRangedKeySet(t *testing.T) {
type testEntry struct { type testEntry struct {
A, B *RangedKeySet A, B *RangedKeySet
Union *RangedKeySet Union *RangedKeySet
...@@ -145,10 +145,10 @@ func TestRangedKeySet(t *testing.T) { ...@@ -145,10 +145,10 @@ func TestRangedKeySet(t *testing.T) {
D := A.Difference(B) D := A.Difference(B)
if !U.Equal(tt.Union) { if !U.Equal(tt.Union) {
t.Errorf("Union:\n A: %s\n B: %s\n ->u: %s\n okU: %s\n", tt.A, tt.B, U, tt.Union) t.Errorf("Union:\n A: %s\n B: %s\n ->u: %s\n okU: %s\n", A, B, U, tt.Union)
} }
if !D.Equal(tt.Difference) { if !D.Equal(tt.Difference) {
t.Errorf("Difference:\n A: %s\n B: %s\n ->d: %s\n okD: %s\n", tt.A, tt.B, D, tt.Difference) t.Errorf("Difference:\n A: %s\n B: %s\n ->d: %s\n okD: %s\n", A, B, D, tt.Difference)
} }
// HasRange // HasRange
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment