improve unit testing cases

This commit is contained in:
John
2020-03-19 23:53:03 +08:00
parent 0b6d04485e
commit 07e65c14a9
54 changed files with 1966 additions and 1746 deletions

View File

@ -541,31 +541,31 @@ func TestArray_RemoveValue(t *testing.T) {
}
func TestArray_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Array *garray.Array
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": []byte(`[1,2,3]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": g.Slice{1, 2, 3},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
}

View File

@ -576,31 +576,31 @@ func TestIntArray_RemoveValue(t *testing.T) {
}
func TestIntArray_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Array *garray.IntArray
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": []byte(`[1,2,3]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": g.Slice{1, 2, 3},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
}

View File

@ -579,31 +579,31 @@ func TestStrArray_RemoveValue(t *testing.T) {
}
func TestStrArray_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Array *garray.StrArray
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": []byte(`["1","2","3"]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"array": g.SliceStr{"1", "2", "3"},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
t.Assert(v.Name, "john")
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
})
}

View File

@ -16,7 +16,7 @@ import (
)
// 检查链表长度
func checkListLen(t *testing.T, l *List, len int) bool {
func checkListLen(t *gtest.T, l *List, len int) bool {
if n := l.Len(); n != len {
t.Errorf("l.Len() = %d, want %d", n, len)
return false
@ -25,7 +25,7 @@ func checkListLen(t *testing.T, l *List, len int) bool {
}
// 检查指针地址
func checkListPointers(t *testing.T, l *List, es []*Element) {
func checkListPointers(t *gtest.T, l *List, es []*Element) {
if !checkListLen(t, l, len(es)) {
return
}
@ -80,87 +80,89 @@ func TestBasic(t *testing.T) {
}
func TestList(t *testing.T) {
l := New()
checkListPointers(t, l, []*Element{})
gtest.C(t, func(t *gtest.T) {
l := New()
checkListPointers(t, l, []*Element{})
// Single element list
e := l.PushFront("a")
checkListPointers(t, l, []*Element{e})
l.MoveToFront(e)
checkListPointers(t, l, []*Element{e})
l.MoveToBack(e)
checkListPointers(t, l, []*Element{e})
l.Remove(e)
checkListPointers(t, l, []*Element{})
// Bigger list
e2 := l.PushFront(2)
e1 := l.PushFront(1)
e3 := l.PushBack(3)
e4 := l.PushBack("banana")
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.Remove(e2)
checkListPointers(t, l, []*Element{e1, e3, e4})
l.MoveToFront(e3) // move from middle
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToFront(e1)
l.MoveToBack(e3) // move from middle
checkListPointers(t, l, []*Element{e1, e4, e3})
l.MoveToFront(e3) // move from back
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToFront(e3) // should be no-op
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToBack(e3) // move from front
checkListPointers(t, l, []*Element{e1, e4, e3})
l.MoveToBack(e3) // should be no-op
checkListPointers(t, l, []*Element{e1, e4, e3})
e2 = l.InsertBefore(e1, 2) // insert before front
checkListPointers(t, l, []*Element{e2, e1, e4, e3})
l.Remove(e2)
e2 = l.InsertBefore(e4, 2) // insert before middle
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
l.Remove(e2)
e2 = l.InsertBefore(e3, 2) // insert before back
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
l.Remove(e2)
e2 = l.InsertAfter(e1, 2) // insert after front
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
l.Remove(e2)
e2 = l.InsertAfter(e4, 2) // insert after middle
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
l.Remove(e2)
e2 = l.InsertAfter(e3, 2) // insert after back
checkListPointers(t, l, []*Element{e1, e4, e3, e2})
l.Remove(e2)
// Check standard iteration.
sum := 0
for e := l.Front(); e != nil; e = e.Next() {
if i, ok := e.Value.(int); ok {
sum += i
}
}
if sum != 4 {
t.Errorf("sum over l = %d, want 4", sum)
}
// Clear all elements by iterating
var next *Element
for e := l.Front(); e != nil; e = next {
next = e.Next()
// Single element list
e := l.PushFront("a")
checkListPointers(t, l, []*Element{e})
l.MoveToFront(e)
checkListPointers(t, l, []*Element{e})
l.MoveToBack(e)
checkListPointers(t, l, []*Element{e})
l.Remove(e)
}
checkListPointers(t, l, []*Element{})
checkListPointers(t, l, []*Element{})
// Bigger list
e2 := l.PushFront(2)
e1 := l.PushFront(1)
e3 := l.PushBack(3)
e4 := l.PushBack("banana")
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.Remove(e2)
checkListPointers(t, l, []*Element{e1, e3, e4})
l.MoveToFront(e3) // move from middle
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToFront(e1)
l.MoveToBack(e3) // move from middle
checkListPointers(t, l, []*Element{e1, e4, e3})
l.MoveToFront(e3) // move from back
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToFront(e3) // should be no-op
checkListPointers(t, l, []*Element{e3, e1, e4})
l.MoveToBack(e3) // move from front
checkListPointers(t, l, []*Element{e1, e4, e3})
l.MoveToBack(e3) // should be no-op
checkListPointers(t, l, []*Element{e1, e4, e3})
e2 = l.InsertBefore(e1, 2) // insert before front
checkListPointers(t, l, []*Element{e2, e1, e4, e3})
l.Remove(e2)
e2 = l.InsertBefore(e4, 2) // insert before middle
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
l.Remove(e2)
e2 = l.InsertBefore(e3, 2) // insert before back
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
l.Remove(e2)
e2 = l.InsertAfter(e1, 2) // insert after front
checkListPointers(t, l, []*Element{e1, e2, e4, e3})
l.Remove(e2)
e2 = l.InsertAfter(e4, 2) // insert after middle
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
l.Remove(e2)
e2 = l.InsertAfter(e3, 2) // insert after back
checkListPointers(t, l, []*Element{e1, e4, e3, e2})
l.Remove(e2)
// Check standard iteration.
sum := 0
for e := l.Front(); e != nil; e = e.Next() {
if i, ok := e.Value.(int); ok {
sum += i
}
}
if sum != 4 {
t.Errorf("sum over l = %d, want 4", sum)
}
// Clear all elements by iterating
var next *Element
for e := l.Front(); e != nil; e = next {
next = e.Next()
l.Remove(e)
}
checkListPointers(t, l, []*Element{})
})
}
func checkList(t *testing.T, l *List, es []interface{}) {
func checkList(t *gtest.T, l *List, es []interface{}) {
if !checkListLen(t, l, len(es)) {
return
}
@ -193,60 +195,64 @@ func checkList(t *testing.T, l *List, es []interface{}) {
}
func TestExtending(t *testing.T) {
l1 := New()
l2 := New()
gtest.C(t, func(t *gtest.T) {
l1 := New()
l2 := New()
l1.PushBack(1)
l1.PushBack(2)
l1.PushBack(3)
l1.PushBack(1)
l1.PushBack(2)
l1.PushBack(3)
l2.PushBack(4)
l2.PushBack(5)
l2.PushBack(4)
l2.PushBack(5)
l3 := New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l2)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
l3 := New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l2)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
l3 = New()
l3.PushFrontList(l2)
checkList(t, l3, []interface{}{4, 5})
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
l3 = New()
l3.PushFrontList(l2)
checkList(t, l3, []interface{}{4, 5})
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3, 4, 5})
checkList(t, l1, []interface{}{1, 2, 3})
checkList(t, l2, []interface{}{4, 5})
checkList(t, l1, []interface{}{1, 2, 3})
checkList(t, l2, []interface{}{4, 5})
l3 = New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l3.PushBackList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushBackList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushFrontList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1, 2, 3})
l3.PushFrontList(l3)
checkList(t, l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = New()
l1.PushBackList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
l1.PushFrontList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
l3 = New()
l1.PushBackList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
l1.PushFrontList(l3)
checkList(t, l1, []interface{}{1, 2, 3})
})
}
func TestRemove(t *testing.T) {
l := New()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
checkListPointers(t, l, []*Element{e1, e2})
//e := l.Front()
//l.Remove(e)
//checkListPointers(t, l, []*Element{e2})
//l.Remove(e)
//checkListPointers(t, l, []*Element{e2})
gtest.C(t, func(t *gtest.T) {
l := New()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
checkListPointers(t, l, []*Element{e1, e2})
//e := l.Front()
//l.Remove(e)
//checkListPointers(t, l, []*Element{e2})
//l.Remove(e)
//checkListPointers(t, l, []*Element{e2})
})
}
func TestIssue4103(t *testing.T) {
@ -289,122 +295,138 @@ func TestIssue6349(t *testing.T) {
}
func TestMove(t *testing.T) {
l := New()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
e3 := l.PushBack(3)
e4 := l.PushBack(4)
gtest.C(t, func(t *gtest.T) {
l := New()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
e3 := l.PushBack(3)
e4 := l.PushBack(4)
l.MoveAfter(e3, e3)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveBefore(e2, e2)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveAfter(e3, e3)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveBefore(e2, e2)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveAfter(e3, e2)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveBefore(e2, e3)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveAfter(e3, e2)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveBefore(e2, e3)
checkListPointers(t, l, []*Element{e1, e2, e3, e4})
l.MoveBefore(e2, e4)
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
e2, e3 = e3, e2
l.MoveBefore(e2, e4)
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
e2, e3 = e3, e2
l.MoveBefore(e4, e1)
checkListPointers(t, l, []*Element{e4, e1, e2, e3})
e1, e2, e3, e4 = e4, e1, e2, e3
l.MoveBefore(e4, e1)
checkListPointers(t, l, []*Element{e4, e1, e2, e3})
e1, e2, e3, e4 = e4, e1, e2, e3
l.MoveAfter(e4, e1)
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
e2, e3, e4 = e4, e2, e3
l.MoveAfter(e4, e1)
checkListPointers(t, l, []*Element{e1, e4, e2, e3})
e2, e3, e4 = e4, e2, e3
l.MoveAfter(e2, e3)
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
e2, e3 = e3, e2
l.MoveAfter(e2, e3)
checkListPointers(t, l, []*Element{e1, e3, e2, e4})
e2, e3 = e3, e2
})
}
// Test PushFront, PushBack, PushFrontList, PushBackList with uninitialized List
func TestZeroList(t *testing.T) {
var l1 = New()
l1.PushFront(1)
checkList(t, l1, []interface{}{1})
gtest.C(t, func(t *gtest.T) {
var l1 = New()
l1.PushFront(1)
checkList(t, l1, []interface{}{1})
var l2 = New()
l2.PushBack(1)
checkList(t, l2, []interface{}{1})
var l2 = New()
l2.PushBack(1)
checkList(t, l2, []interface{}{1})
var l3 = New()
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1})
var l3 = New()
l3.PushFrontList(l1)
checkList(t, l3, []interface{}{1})
var l4 = New()
l4.PushBackList(l2)
checkList(t, l4, []interface{}{1})
var l4 = New()
l4.PushBackList(l2)
checkList(t, l4, []interface{}{1})
})
}
// Test that a list l is not modified when calling InsertBefore with a mark that is not an element of l.
func TestInsertBeforeUnknownMark(t *testing.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertBefore(new(Element), 1)
checkList(t, l, []interface{}{1, 2, 3})
gtest.C(t, func(t *gtest.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertBefore(new(Element), 1)
checkList(t, l, []interface{}{1, 2, 3})
})
}
// Test that a list l is not modified when calling InsertAfter with a mark that is not an element of l.
func TestInsertAfterUnknownMark(t *testing.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertAfter(new(Element), 1)
checkList(t, l, []interface{}{1, 2, 3})
gtest.C(t, func(t *gtest.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertAfter(new(Element), 1)
checkList(t, l, []interface{}{1, 2, 3})
})
}
// Test that a list l is not modified when calling MoveAfter or MoveBefore with a mark that is not an element of l.
func TestMoveUnknownMark(t *testing.T) {
l1 := New()
e1 := l1.PushBack(1)
gtest.C(t, func(t *gtest.T) {
l1 := New()
e1 := l1.PushBack(1)
l2 := New()
e2 := l2.PushBack(2)
l2 := New()
e2 := l2.PushBack(2)
l1.MoveAfter(e1, e2)
checkList(t, l1, []interface{}{1})
checkList(t, l2, []interface{}{2})
l1.MoveAfter(e1, e2)
checkList(t, l1, []interface{}{1})
checkList(t, l2, []interface{}{2})
l1.MoveBefore(e1, e2)
checkList(t, l1, []interface{}{1})
checkList(t, l2, []interface{}{2})
l1.MoveBefore(e1, e2)
checkList(t, l1, []interface{}{1})
checkList(t, l2, []interface{}{2})
})
}
func TestList_RemoveAll(t *testing.T) {
l := New()
l.PushBack(1)
l.RemoveAll()
checkList(t, l, []interface{}{})
l.PushBack(2)
checkList(t, l, []interface{}{2})
gtest.C(t, func(t *gtest.T) {
l := New()
l.PushBack(1)
l.RemoveAll()
checkList(t, l, []interface{}{})
l.PushBack(2)
checkList(t, l, []interface{}{2})
})
}
func TestList_PushFronts(t *testing.T) {
l := New()
a1 := []interface{}{1, 2}
l.PushFronts(a1)
checkList(t, l, []interface{}{2, 1})
a1 = []interface{}{3, 4, 5}
l.PushFronts(a1)
checkList(t, l, []interface{}{5, 4, 3, 2, 1})
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2}
l.PushFronts(a1)
checkList(t, l, []interface{}{2, 1})
a1 = []interface{}{3, 4, 5}
l.PushFronts(a1)
checkList(t, l, []interface{}{5, 4, 3, 2, 1})
})
}
func TestList_PushBacks(t *testing.T) {
l := New()
a1 := []interface{}{1, 2}
l.PushBacks(a1)
checkList(t, l, []interface{}{1, 2})
a1 = []interface{}{3, 4, 5}
l.PushBacks(a1)
checkList(t, l, []interface{}{1, 2, 3, 4, 5})
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2}
l.PushBacks(a1)
checkList(t, l, []interface{}{1, 2})
a1 = []interface{}{3, 4, 5}
l.PushBacks(a1)
checkList(t, l, []interface{}{1, 2, 3, 4, 5})
})
}
func TestList_PopBacks(t *testing.T) {
@ -423,165 +445,190 @@ func TestList_PopBacks(t *testing.T) {
}
func TestList_PopFronts(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopFronts(2)
t.Assert(i1, []interface{}{4, 3})
t.Assert(l.Len(), 2)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopFronts(2)
t.Assert(i1, []interface{}{4, 3})
t.Assert(l.Len(), 2)
})
}
func TestList_PopBackAll(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopBackAll()
t.Assert(i1, []interface{}{1, 2, 3, 4})
t.Assert(l.Len(), 0)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopBackAll()
t.Assert(i1, []interface{}{1, 2, 3, 4})
t.Assert(l.Len(), 0)
})
}
func TestList_PopFrontAll(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopFrontAll()
t.Assert(i1, []interface{}{4, 3, 2, 1})
t.Assert(l.Len(), 0)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.PopFrontAll()
t.Assert(i1, []interface{}{4, 3, 2, 1})
t.Assert(l.Len(), 0)
})
}
func TestList_FrontAll(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.FrontAll()
t.Assert(i1, []interface{}{4, 3, 2, 1})
t.Assert(l.Len(), 4)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.FrontAll()
t.Assert(i1, []interface{}{4, 3, 2, 1})
t.Assert(l.Len(), 4)
})
}
func TestList_BackAll(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.BackAll()
t.Assert(i1, []interface{}{1, 2, 3, 4})
t.Assert(l.Len(), 4)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.BackAll()
t.Assert(i1, []interface{}{1, 2, 3, 4})
t.Assert(l.Len(), 4)
})
}
func TestList_FrontValue(t *testing.T) {
l := New()
l2 := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.FrontValue()
t.Assert(gconv.Int(i1), 4)
t.Assert(l.Len(), 4)
gtest.C(t, func(t *gtest.T) {
l := New()
l2 := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.FrontValue()
t.Assert(gconv.Int(i1), 4)
t.Assert(l.Len(), 4)
i1 = l2.FrontValue()
t.Assert(i1, nil)
i1 = l2.FrontValue()
t.Assert(i1, nil)
})
}
func TestList_BackValue(t *testing.T) {
l := New()
l2 := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.BackValue()
t.Assert(gconv.Int(i1), 1)
t.Assert(l.Len(), 4)
i1 = l2.FrontValue()
t.Assert(i1, nil)
gtest.C(t, func(t *gtest.T) {
l := New()
l2 := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
i1 := l.BackValue()
t.Assert(gconv.Int(i1), 1)
t.Assert(l.Len(), 4)
i1 = l2.FrontValue()
t.Assert(i1, nil)
})
}
func TestList_Back(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
t.Assert(e1.Value, 1)
t.Assert(l.Len(), 4)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
t.Assert(e1.Value, 1)
t.Assert(l.Len(), 4)
})
}
func TestList_Size(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
t.Assert(l.Size(), 4)
l.PopFront()
t.Assert(l.Size(), 3)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
t.Assert(l.Size(), 4)
l.PopFront()
t.Assert(l.Size(), 3)
})
}
func TestList_Removes(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
l.Removes([]*Element{e1})
t.Assert(l.Len(), 3)
e2 := l.Back()
l.Removes([]*Element{e2})
t.Assert(l.Len(), 2)
checkList(t, l, []interface{}{4, 3})
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
l.Removes([]*Element{e1})
t.Assert(l.Len(), 3)
e2 := l.Back()
l.Removes([]*Element{e2})
t.Assert(l.Len(), 2)
checkList(t, l, []interface{}{4, 3})
})
}
func TestList_Clear(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
l.Clear()
t.Assert(l.Len(), 0)
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
l.Clear()
t.Assert(l.Len(), 0)
})
}
func TestList_IteratorAsc(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 5, 6, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 2 {
return true
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 5, 6, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 2 {
return true
}
return false
}
return false
}
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
l.IteratorAsc(fun1)
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
l.IteratorAsc(fun1)
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
})
}
func TestList_IteratorDesc(t *testing.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 6 {
return true
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{1, 2, 3, 4}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 6 {
return true
}
return false
}
return false
}
l.IteratorDesc(fun1)
t.Assert(l.Len(), 4)
checkList(t, l, []interface{}{4, 3, 2, 1})
l.IteratorDesc(fun1)
t.Assert(l.Len(), 4)
checkList(t, l, []interface{}{4, 3, 2, 1})
})
}
func TestList_Iterator(t *testing.T) {
l := New()
a1 := []interface{}{"a", "b", "c", "d", "e"}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.String(e1.Value) > "c" {
return true
gtest.C(t, func(t *gtest.T) {
l := New()
a1 := []interface{}{"a", "b", "c", "d", "e"}
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.String(e1.Value) > "c" {
return true
}
return false
}
return false
}
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
l.Iterator(fun1)
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
l.Iterator(fun1)
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
})
}
func TestList_Join(t *testing.T) {
@ -634,30 +681,30 @@ func TestList_Json(t *testing.T) {
}
func TestList_UnmarshalValue(t *testing.T) {
type T struct {
type TList struct {
Name string
List *List
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var tlist *TList
err := gconv.Struct(map[string]interface{}{
"name": "john",
"list": []byte(`[1,2,3]`),
}, &t)
}, &tlist)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
t.Assert(tlist.Name, "john")
t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3})
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var tlist *TList
err := gconv.Struct(map[string]interface{}{
"name": "john",
"list": []interface{}{1, 2, 3},
}, &t)
}, &tlist)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
t.Assert(tlist.Name, "john")
t.Assert(tlist.List.FrontAll(), []interface{}{1, 2, 3})
})
}

View File

@ -55,61 +55,68 @@ func Test_AnyAnyMap_Basic(t *testing.T) {
}
func Test_AnyAnyMap_Set_Fun(t *testing.T) {
m := gmap.NewAnyAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
})
}
func Test_AnyAnyMap_Batch(t *testing.T) {
m := gmap.NewAnyAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]interface{}{1, 2})
t.Assert(m.Map(), map[interface{}]interface{}{3: 3})
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]interface{}{1, 2})
t.Assert(m.Map(), map[interface{}]interface{}{3: 3})
})
}
func Test_AnyAnyMap_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, "2")
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, "2")
t.Assert(j, 1)
}
func Test_AnyAnyMap_Lock(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.LockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.LockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[interface{}]interface{}) {
t.Assert(m, expect)
})
})
}
@ -130,53 +137,61 @@ func Test_AnyAnyMap_Clone(t *testing.T) {
}
func Test_AnyAnyMap_Merge(t *testing.T) {
m1 := gmap.NewAnyAnyMap()
m2 := gmap.NewAnyAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewAnyAnyMap()
m2 := gmap.NewAnyAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
})
}
func Test_AnyAnyMap_Map(t *testing.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
})
}
func Test_AnyAnyMap_MapCopy(t *testing.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), nil)
m.Set(4, 4)
t.Assert(data[4], nil)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), nil)
m.Set(4, 4)
t.Assert(data[4], nil)
})
}
func Test_AnyAnyMap_FilterEmpty(t *testing.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Get(1), nil)
t.Assert(m.Get(2), 2)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Get(1), nil)
t.Assert(m.Get(2), 2)
})
}
func Test_AnyAnyMap_Json(t *testing.T) {
@ -277,37 +292,37 @@ func Test_AnyAnyMap_Pops(t *testing.T) {
}
func TestAnyAnyMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.Map
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":"v1","k2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": "v1",
"k2": "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
}

View File

@ -56,124 +56,141 @@ func Test_IntAnyMap_Basic(t *testing.T) {
})
}
func Test_IntAnyMap_Set_Fun(t *testing.T) {
m := gmap.NewIntAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntAnyMap()
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
t.Assert(m.SetIfNotExistFunc(1, getAny), false)
t.Assert(m.SetIfNotExistFunc(3, getAny), true)
t.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
t.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
})
}
func Test_IntAnyMap_Batch(t *testing.T) {
m := gmap.NewIntAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntAnyMap()
m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3})
t.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]interface{}{3: 3})
m.Sets(map[int]interface{}{1: 1, 2: "2", 3: 3})
t.Assert(m.Map(), map[int]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]interface{}{3: 3})
})
}
func Test_IntAnyMap_Iterator(t *testing.T) {
expect := map[int]interface{}{1: 1, 2: "2"}
m := gmap.NewIntAnyMapFrom(expect)
m.Iterator(func(k int, v interface{}) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[int]interface{}{1: 1, 2: "2"}
m := gmap.NewIntAnyMapFrom(expect)
m.Iterator(func(k int, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v interface{}) bool {
i++
return true
})
m.Iterator(func(k int, v interface{}) bool {
j++
return false
})
t.Assert(i, "2")
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v interface{}) bool {
i++
return true
})
m.Iterator(func(k int, v interface{}) bool {
j++
return false
})
t.Assert(i, "2")
t.Assert(j, 1)
}
func Test_IntAnyMap_Lock(t *testing.T) {
expect := map[int]interface{}{1: 1, 2: "2"}
m := gmap.NewIntAnyMapFrom(expect)
m.LockFunc(func(m map[int]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[int]interface{}) {
t.Assert(m, expect)
gtest.C(t, func(t *gtest.T) {
expect := map[int]interface{}{1: 1, 2: "2"}
m := gmap.NewIntAnyMapFrom(expect)
m.LockFunc(func(m map[int]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[int]interface{}) {
t.Assert(m, expect)
})
})
}
func Test_IntAnyMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: "2"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
})
}
func Test_IntAnyMap_Merge(t *testing.T) {
m1 := gmap.NewIntAnyMap()
m2 := gmap.NewIntAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewIntAnyMap()
m2 := gmap.NewIntAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[int]interface{}{1: 1, 2: "2"})
})
}
func Test_IntAnyMap_Map(t *testing.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
})
}
func Test_IntAnyMap_MapCopy(t *testing.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), nil)
m.Set(4, 4)
t.Assert(data[4], nil)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), nil)
m.Set(4, 4)
t.Assert(data[4], nil)
})
}
func Test_IntAnyMap_FilterEmpty(t *testing.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), 2)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntAnyMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), 2)
})
}
func Test_IntAnyMap_Json(t *testing.T) {
@ -260,37 +277,37 @@ func Test_IntAnyMap_Pops(t *testing.T) {
}
func TestIntAnyMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.IntAnyMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"1":"v1","2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "v1")
t.Assert(t.Map.Get(2), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "v1")
t.Assert(v.Map.Get(2), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.MapIntAny{
1: "v1",
2: "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "v1")
t.Assert(t.Map.Get(2), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "v1")
t.Assert(v.Map.Get(2), "v2")
})
}

View File

@ -56,127 +56,143 @@ func Test_IntIntMap_Basic(t *testing.T) {
})
}
func Test_IntIntMap_Set_Fun(t *testing.T) {
m := gmap.NewIntIntMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntIntMap()
m.GetOrSetFunc(1, getInt)
m.GetOrSetFuncLock(2, getInt)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
t.Assert(m.SetIfNotExistFunc(1, getInt), false)
t.Assert(m.SetIfNotExistFunc(3, getInt), true)
t.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
t.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
m.GetOrSetFunc(1, getInt)
m.GetOrSetFuncLock(2, getInt)
t.Assert(m.Get(1), 123)
t.Assert(m.Get(2), 123)
t.Assert(m.SetIfNotExistFunc(1, getInt), false)
t.Assert(m.SetIfNotExistFunc(3, getInt), true)
t.Assert(m.SetIfNotExistFuncLock(2, getInt), false)
t.Assert(m.SetIfNotExistFuncLock(4, getInt), true)
})
}
func Test_IntIntMap_Batch(t *testing.T) {
m := gmap.NewIntIntMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntIntMap()
m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
m.Iterator(intIntCallBack)
t.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]int{3: 3})
m.Sets(map[int]int{1: 1, 2: 2, 3: 3})
m.Iterator(intIntCallBack)
t.Assert(m.Map(), map[int]int{1: 1, 2: 2, 3: 3})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]int{3: 3})
})
}
func Test_IntIntMap_Iterator(t *testing.T) {
expect := map[int]int{1: 1, 2: 2}
m := gmap.NewIntIntMapFrom(expect)
m.Iterator(func(k int, v int) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[int]int{1: 1, 2: 2}
m := gmap.NewIntIntMapFrom(expect)
m.Iterator(func(k int, v int) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v int) bool {
i++
return true
})
m.Iterator(func(k int, v int) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v int) bool {
i++
return true
})
m.Iterator(func(k int, v int) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_IntIntMap_Lock(t *testing.T) {
expect := map[int]int{1: 1, 2: 2}
m := gmap.NewIntIntMapFrom(expect)
m.LockFunc(func(m map[int]int) {
t.Assert(m, expect)
gtest.C(t, func(t *gtest.T) {
expect := map[int]int{1: 1, 2: 2}
m := gmap.NewIntIntMapFrom(expect)
m.LockFunc(func(m map[int]int) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[int]int) {
t.Assert(m, expect)
})
})
m.RLockFunc(func(m map[int]int) {
t.Assert(m, expect)
})
}
func Test_IntIntMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
})
}
func Test_IntIntMap_Merge(t *testing.T) {
m1 := gmap.NewIntIntMap()
m2 := gmap.NewIntIntMap()
m1.Set(1, 1)
m2.Set(2, 2)
m1.Merge(m2)
t.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewIntIntMap()
m2 := gmap.NewIntIntMap()
m1.Set(1, 1)
m2.Set(2, 2)
m1.Merge(m2)
t.Assert(m1.Map(), map[int]int{1: 1, 2: 2})
})
}
func Test_IntIntMap_Map(t *testing.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.Map()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 3)
m.Set(4, 4)
t.Assert(data[4], 4)
})
}
func Test_IntIntMap_MapCopy(t *testing.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 0)
m.Set(4, 4)
t.Assert(data[4], 0)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
data := m.MapCopy()
t.Assert(data[1], 0)
t.Assert(data[2], 2)
data[3] = 3
t.Assert(m.Get(3), 0)
m.Set(4, 4)
t.Assert(data[4], 0)
})
}
func Test_IntIntMap_FilterEmpty(t *testing.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), 2)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntIntMap()
m.Set(1, 0)
m.Set(2, 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get(1), 0)
t.Assert(m.Get(2), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), 2)
})
}
func Test_IntIntMap_Json(t *testing.T) {
@ -263,37 +279,37 @@ func Test_IntIntMap_Pops(t *testing.T) {
}
func TestIntIntMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.IntIntMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"1":1,"2":2}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "1")
t.Assert(t.Map.Get(2), "2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "1")
t.Assert(v.Map.Get(2), "2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.MapIntAny{
1: 1,
2: 2,
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "1")
t.Assert(t.Map.Get(2), "2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "1")
t.Assert(v.Map.Get(2), "2")
})
}

View File

@ -61,123 +61,137 @@ func Test_IntStrMap_Basic(t *testing.T) {
})
}
func Test_IntStrMap_Set_Fun(t *testing.T) {
m := gmap.NewIntStrMap()
m.GetOrSetFunc(1, getStr)
m.GetOrSetFuncLock(2, getStr)
t.Assert(m.Get(1), "z")
t.Assert(m.Get(2), "z")
t.Assert(m.SetIfNotExistFunc(1, getStr), false)
t.Assert(m.SetIfNotExistFunc(3, getStr), true)
t.Assert(m.SetIfNotExistFuncLock(2, getStr), false)
t.Assert(m.SetIfNotExistFuncLock(4, getStr), true)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntStrMap()
m.GetOrSetFunc(1, getStr)
m.GetOrSetFuncLock(2, getStr)
t.Assert(m.Get(1), "z")
t.Assert(m.Get(2), "z")
t.Assert(m.SetIfNotExistFunc(1, getStr), false)
t.Assert(m.SetIfNotExistFunc(3, getStr), true)
t.Assert(m.SetIfNotExistFuncLock(2, getStr), false)
t.Assert(m.SetIfNotExistFuncLock(4, getStr), true)
})
}
func Test_IntStrMap_Batch(t *testing.T) {
m := gmap.NewIntStrMap()
m.Sets(map[int]string{1: "a", 2: "b", 3: "c"})
t.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]interface{}{3: "c"})
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntStrMap()
m.Sets(map[int]string{1: "a", 2: "b", 3: "c"})
t.Assert(m.Map(), map[int]string{1: "a", 2: "b", 3: "c"})
m.Removes([]int{1, 2})
t.Assert(m.Map(), map[int]interface{}{3: "c"})
})
}
func Test_IntStrMap_Iterator(t *testing.T) {
expect := map[int]string{1: "a", 2: "b"}
m := gmap.NewIntStrMapFrom(expect)
m.Iterator(func(k int, v string) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[int]string{1: "a", 2: "b"}
m := gmap.NewIntStrMapFrom(expect)
m.Iterator(func(k int, v string) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v string) bool {
i++
return true
})
m.Iterator(func(k int, v string) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k int, v string) bool {
i++
return true
})
m.Iterator(func(k int, v string) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_IntStrMap_Lock(t *testing.T) {
expect := map[int]string{1: "a", 2: "b", 3: "c"}
m := gmap.NewIntStrMapFrom(expect)
m.LockFunc(func(m map[int]string) {
t.Assert(m, expect)
gtest.C(t, func(t *gtest.T) {
expect := map[int]string{1: "a", 2: "b", 3: "c"}
m := gmap.NewIntStrMapFrom(expect)
m.LockFunc(func(m map[int]string) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[int]string) {
t.Assert(m, expect)
})
})
m.RLockFunc(func(m map[int]string) {
t.Assert(m, expect)
})
}
func Test_IntStrMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewIntStrMapFrom(map[int]string{1: "a", 2: "b", 3: "c"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
t.AssertIN(2, m.Keys())
})
}
func Test_IntStrMap_Merge(t *testing.T) {
m1 := gmap.NewIntStrMap()
m2 := gmap.NewIntStrMap()
m1.Set(1, "a")
m2.Set(2, "b")
m1.Merge(m2)
t.Assert(m1.Map(), map[int]string{1: "a", 2: "b"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewIntStrMap()
m2 := gmap.NewIntStrMap()
m1.Set(1, "a")
m2.Set(2, "b")
m1.Merge(m2)
t.Assert(m1.Map(), map[int]string{1: "a", 2: "b"})
})
}
func Test_IntStrMap_Map(t *testing.T) {
m := gmap.NewIntStrMap()
m.Set(1, "0")
m.Set(2, "2")
t.Assert(m.Get(1), "0")
t.Assert(m.Get(2), "2")
data := m.Map()
t.Assert(data[1], "0")
t.Assert(data[2], "2")
data[3] = "3"
t.Assert(m.Get(3), "3")
m.Set(4, "4")
t.Assert(data[4], "4")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntStrMap()
m.Set(1, "0")
m.Set(2, "2")
t.Assert(m.Get(1), "0")
t.Assert(m.Get(2), "2")
data := m.Map()
t.Assert(data[1], "0")
t.Assert(data[2], "2")
data[3] = "3"
t.Assert(m.Get(3), "3")
m.Set(4, "4")
t.Assert(data[4], "4")
})
}
func Test_IntStrMap_MapCopy(t *testing.T) {
m := gmap.NewIntStrMap()
m.Set(1, "0")
m.Set(2, "2")
t.Assert(m.Get(1), "0")
t.Assert(m.Get(2), "2")
data := m.MapCopy()
t.Assert(data[1], "0")
t.Assert(data[2], "2")
data[3] = "3"
t.Assert(m.Get(3), "")
m.Set(4, "4")
t.Assert(data[4], "")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntStrMap()
m.Set(1, "0")
m.Set(2, "2")
t.Assert(m.Get(1), "0")
t.Assert(m.Get(2), "2")
data := m.MapCopy()
t.Assert(data[1], "0")
t.Assert(data[2], "2")
data[3] = "3"
t.Assert(m.Get(3), "")
m.Set(4, "4")
t.Assert(data[4], "")
})
}
func Test_IntStrMap_FilterEmpty(t *testing.T) {
m := gmap.NewIntStrMap()
m.Set(1, "")
m.Set(2, "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get(2), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), "2")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewIntStrMap()
m.Set(1, "")
m.Set(2, "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get(2), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), "2")
})
}
func Test_IntStrMap_Json(t *testing.T) {
@ -264,37 +278,37 @@ func Test_IntStrMap_Pops(t *testing.T) {
}
func TestIntStrMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.IntStrMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"1":"v1","2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "v1")
t.Assert(t.Map.Get(2), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "v1")
t.Assert(v.Map.Get(2), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.MapIntAny{
1: "v1",
2: "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get(1), "v1")
t.Assert(t.Map.Get(2), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get(1), "v1")
t.Assert(v.Map.Get(2), "v2")
})
}

View File

@ -52,86 +52,100 @@ func Test_ListMap_Basic(t *testing.T) {
})
}
func Test_ListMap_Set_Fun(t *testing.T) {
m := gmap.NewListMap()
m.GetOrSetFunc("fun", getValue)
m.GetOrSetFuncLock("funlock", getValue)
t.Assert(m.Get("funlock"), 3)
t.Assert(m.Get("fun"), 3)
m.GetOrSetFunc("fun", getValue)
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewListMap()
m.GetOrSetFunc("fun", getValue)
m.GetOrSetFuncLock("funlock", getValue)
t.Assert(m.Get("funlock"), 3)
t.Assert(m.Get("fun"), 3)
m.GetOrSetFunc("fun", getValue)
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
})
}
func Test_ListMap_Batch(t *testing.T) {
m := gmap.NewListMap()
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
gtest.C(t, func(t *gtest.T) {
m := gmap.NewListMap()
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
})
}
func Test_ListMap_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
m := gmap.NewListMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
m := gmap.NewListMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_ListMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewListMapFrom(map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
})
}
func Test_ListMap_Basic_Merge(t *testing.T) {
m1 := gmap.NewListMap()
m2 := gmap.NewListMap()
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(m2)
t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewListMap()
m2 := gmap.NewListMap()
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(m2)
t.Assert(m1.Map(), map[interface{}]interface{}{"key1": "val1", "key2": "val2"})
})
}
func Test_ListMap_Order(t *testing.T) {
m := gmap.NewListMap()
m.Set("k1", "v1")
m.Set("k2", "v2")
m.Set("k3", "v3")
t.Assert(m.Keys(), g.Slice{"k1", "k2", "k3"})
t.Assert(m.Values(), g.Slice{"v1", "v2", "v3"})
gtest.C(t, func(t *gtest.T) {
m := gmap.NewListMap()
m.Set("k1", "v1")
m.Set("k2", "v2")
m.Set("k3", "v3")
t.Assert(m.Keys(), g.Slice{"k1", "k2", "k3"})
t.Assert(m.Values(), g.Slice{"v1", "v2", "v3"})
})
}
func Test_ListMap_FilterEmpty(t *testing.T) {
m := gmap.NewListMap()
m.Set(1, "")
m.Set(2, "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get(2), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), "2")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewListMap()
m.Set(1, "")
m.Set(2, "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get(2), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get(2), "2")
})
}
func Test_ListMap_Json(t *testing.T) {
@ -233,37 +247,37 @@ func Test_ListMap_Pops(t *testing.T) {
}
func TestListMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.ListMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"1":"v1","2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("1"), "v1")
t.Assert(t.Map.Get("2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("1"), "v1")
t.Assert(v.Map.Get("2"), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.MapIntAny{
1: "v1",
2: "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("1"), "v1")
t.Assert(t.Map.Get("2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("1"), "v1")
t.Assert(v.Map.Get("2"), "v2")
})
}

View File

@ -54,124 +54,141 @@ func Test_StrAnyMap_Basic(t *testing.T) {
})
}
func Test_StrAnyMap_Set_Fun(t *testing.T) {
m := gmap.NewStrAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.GetOrSetFunc("a", getAny)
m.GetOrSetFuncLock("b", getAny)
t.Assert(m.Get("a"), 123)
t.Assert(m.Get("b"), 123)
t.Assert(m.SetIfNotExistFunc("a", getAny), false)
t.Assert(m.SetIfNotExistFunc("c", getAny), true)
t.Assert(m.SetIfNotExistFuncLock("b", getAny), false)
t.Assert(m.SetIfNotExistFuncLock("d", getAny), true)
m.GetOrSetFunc("a", getAny)
m.GetOrSetFuncLock("b", getAny)
t.Assert(m.Get("a"), 123)
t.Assert(m.Get("b"), 123)
t.Assert(m.SetIfNotExistFunc("a", getAny), false)
t.Assert(m.SetIfNotExistFunc("c", getAny), true)
t.Assert(m.SetIfNotExistFuncLock("b", getAny), false)
t.Assert(m.SetIfNotExistFuncLock("d", getAny), true)
})
}
func Test_StrAnyMap_Batch(t *testing.T) {
m := gmap.NewStrAnyMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3})
t.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]interface{}{"c": 3})
m.Sets(map[string]interface{}{"a": 1, "b": "2", "c": 3})
t.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]interface{}{"c": 3})
})
}
func Test_StrAnyMap_Iterator(t *testing.T) {
expect := map[string]interface{}{"a": true, "b": false}
m := gmap.NewStrAnyMapFrom(expect)
m.Iterator(func(k string, v interface{}) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[string]interface{}{"a": true, "b": false}
m := gmap.NewStrAnyMapFrom(expect)
m.Iterator(func(k string, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v interface{}) bool {
i++
return true
})
m.Iterator(func(k string, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v interface{}) bool {
i++
return true
})
m.Iterator(func(k string, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_StrAnyMap_Lock(t *testing.T) {
expect := map[string]interface{}{"a": true, "b": false}
gtest.C(t, func(t *gtest.T) {
expect := map[string]interface{}{"a": true, "b": false}
m := gmap.NewStrAnyMapFrom(expect)
m.LockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
m := gmap.NewStrAnyMapFrom(expect)
m.LockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]interface{}) {
t.Assert(m, expect)
})
})
}
func Test_StrAnyMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"a": 1, "b": "2"})
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
})
}
func Test_StrAnyMap_Merge(t *testing.T) {
m1 := gmap.NewStrAnyMap()
m2 := gmap.NewStrAnyMap()
m1.Set("a", 1)
m2.Set("b", "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrAnyMap()
m2 := gmap.NewStrAnyMap()
m1.Set("a", 1)
m2.Set("b", "2")
m1.Merge(m2)
t.Assert(m1.Map(), map[string]interface{}{"a": 1, "b": "2"})
})
}
func Test_StrAnyMap_Map(t *testing.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.Map()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 3)
m.Set("4", 4)
t.Assert(data["4"], 4)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.Map()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 3)
m.Set("4", 4)
t.Assert(data["4"], 4)
})
}
func Test_StrAnyMap_MapCopy(t *testing.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.MapCopy()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), nil)
m.Set("4", 4)
t.Assert(data["4"], nil)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.MapCopy()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), nil)
m.Set("4", 4)
t.Assert(data["4"], nil)
})
}
func Test_StrAnyMap_FilterEmpty(t *testing.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 0)
m.Set("2", 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), 0)
t.Assert(m.Get("2"), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), 2)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrAnyMap()
m.Set("1", 0)
m.Set("2", 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), 0)
t.Assert(m.Get("2"), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), 2)
})
}
func Test_StrAnyMap_Json(t *testing.T) {
@ -272,37 +289,37 @@ func Test_StrAnyMap_Pops(t *testing.T) {
}
func TestStrAnyMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.StrAnyMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":"v1","k2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": "v1",
"k2": "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
}

View File

@ -56,125 +56,141 @@ func Test_StrIntMap_Basic(t *testing.T) {
})
}
func Test_StrIntMap_Set_Fun(t *testing.T) {
m := gmap.NewStrIntMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrIntMap()
m.GetOrSetFunc("a", getInt)
m.GetOrSetFuncLock("b", getInt)
t.Assert(m.Get("a"), 123)
t.Assert(m.Get("b"), 123)
t.Assert(m.SetIfNotExistFunc("a", getInt), false)
t.Assert(m.SetIfNotExistFunc("c", getInt), true)
t.Assert(m.SetIfNotExistFuncLock("b", getInt), false)
t.Assert(m.SetIfNotExistFuncLock("d", getInt), true)
m.GetOrSetFunc("a", getInt)
m.GetOrSetFuncLock("b", getInt)
t.Assert(m.Get("a"), 123)
t.Assert(m.Get("b"), 123)
t.Assert(m.SetIfNotExistFunc("a", getInt), false)
t.Assert(m.SetIfNotExistFunc("c", getInt), true)
t.Assert(m.SetIfNotExistFuncLock("b", getInt), false)
t.Assert(m.SetIfNotExistFuncLock("d", getInt), true)
})
}
func Test_StrIntMap_Batch(t *testing.T) {
m := gmap.NewStrIntMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrIntMap()
m.Sets(map[string]int{"a": 1, "b": 2, "c": 3})
t.Assert(m.Map(), map[string]int{"a": 1, "b": 2, "c": 3})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]int{"c": 3})
m.Sets(map[string]int{"a": 1, "b": 2, "c": 3})
t.Assert(m.Map(), map[string]int{"a": 1, "b": 2, "c": 3})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]int{"c": 3})
})
}
func Test_StrIntMap_Iterator(t *testing.T) {
expect := map[string]int{"a": 1, "b": 2}
m := gmap.NewStrIntMapFrom(expect)
m.Iterator(func(k string, v int) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[string]int{"a": 1, "b": 2}
m := gmap.NewStrIntMapFrom(expect)
m.Iterator(func(k string, v int) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v int) bool {
i++
return true
})
m.Iterator(func(k string, v int) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v int) bool {
i++
return true
})
m.Iterator(func(k string, v int) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_StrIntMap_Lock(t *testing.T) {
expect := map[string]int{"a": 1, "b": 2}
gtest.C(t, func(t *gtest.T) {
expect := map[string]int{"a": 1, "b": 2}
m := gmap.NewStrIntMapFrom(expect)
m.LockFunc(func(m map[string]int) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]int) {
t.Assert(m, expect)
m := gmap.NewStrIntMapFrom(expect)
m.LockFunc(func(m map[string]int) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]int) {
t.Assert(m, expect)
})
})
}
func Test_StrIntMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewStrIntMapFrom(map[string]int{"a": 1, "b": 2, "c": 3})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewStrIntMapFrom(map[string]int{"a": 1, "b": 2, "c": 3})
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
})
}
func Test_StrIntMap_Merge(t *testing.T) {
m1 := gmap.NewStrIntMap()
m2 := gmap.NewStrIntMap()
m1.Set("a", 1)
m2.Set("b", 2)
m1.Merge(m2)
t.Assert(m1.Map(), map[string]int{"a": 1, "b": 2})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrIntMap()
m2 := gmap.NewStrIntMap()
m1.Set("a", 1)
m2.Set("b", 2)
m1.Merge(m2)
t.Assert(m1.Map(), map[string]int{"a": 1, "b": 2})
})
}
func Test_StrIntMap_Map(t *testing.T) {
m := gmap.NewStrIntMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.Map()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 3)
m.Set("4", 4)
t.Assert(data["4"], 4)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrIntMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.Map()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 3)
m.Set("4", 4)
t.Assert(data["4"], 4)
})
}
func Test_StrIntMap_MapCopy(t *testing.T) {
m := gmap.NewStrIntMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.MapCopy()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 0)
m.Set("4", 4)
t.Assert(data["4"], 0)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrIntMap()
m.Set("1", 1)
m.Set("2", 2)
t.Assert(m.Get("1"), 1)
t.Assert(m.Get("2"), 2)
data := m.MapCopy()
t.Assert(data["1"], 1)
t.Assert(data["2"], 2)
data["3"] = 3
t.Assert(m.Get("3"), 0)
m.Set("4", 4)
t.Assert(data["4"], 0)
})
}
func Test_StrIntMap_FilterEmpty(t *testing.T) {
m := gmap.NewStrIntMap()
m.Set("1", 0)
m.Set("2", 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), 0)
t.Assert(m.Get("2"), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), 2)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrIntMap()
m.Set("1", 0)
m.Set("2", 2)
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), 0)
t.Assert(m.Get("2"), 2)
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), 2)
})
}
func Test_StrIntMap_Json(t *testing.T) {
@ -275,37 +291,37 @@ func Test_StrIntMap_Pops(t *testing.T) {
}
func TestStrIntMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.StrIntMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":1,"k2":2}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), 1)
t.Assert(t.Map.Get("k2"), 2)
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), 1)
t.Assert(v.Map.Get("k2"), 2)
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": 1,
"k2": 2,
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), 1)
t.Assert(t.Map.Get("k2"), 2)
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), 1)
t.Assert(v.Map.Get("k2"), 2)
})
}

View File

@ -55,123 +55,140 @@ func Test_StrStrMap_Basic(t *testing.T) {
})
}
func Test_StrStrMap_Set_Fun(t *testing.T) {
m := gmap.NewStrStrMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrStrMap()
m.GetOrSetFunc("a", getStr)
m.GetOrSetFuncLock("b", getStr)
t.Assert(m.Get("a"), "z")
t.Assert(m.Get("b"), "z")
t.Assert(m.SetIfNotExistFunc("a", getStr), false)
t.Assert(m.SetIfNotExistFunc("c", getStr), true)
t.Assert(m.SetIfNotExistFuncLock("b", getStr), false)
t.Assert(m.SetIfNotExistFuncLock("d", getStr), true)
m.GetOrSetFunc("a", getStr)
m.GetOrSetFuncLock("b", getStr)
t.Assert(m.Get("a"), "z")
t.Assert(m.Get("b"), "z")
t.Assert(m.SetIfNotExistFunc("a", getStr), false)
t.Assert(m.SetIfNotExistFunc("c", getStr), true)
t.Assert(m.SetIfNotExistFuncLock("b", getStr), false)
t.Assert(m.SetIfNotExistFuncLock("d", getStr), true)
})
}
func Test_StrStrMap_Batch(t *testing.T) {
m := gmap.NewStrStrMap()
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrStrMap()
m.Sets(map[string]string{"a": "a", "b": "b", "c": "c"})
t.Assert(m.Map(), map[string]string{"a": "a", "b": "b", "c": "c"})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]string{"c": "c"})
m.Sets(map[string]string{"a": "a", "b": "b", "c": "c"})
t.Assert(m.Map(), map[string]string{"a": "a", "b": "b", "c": "c"})
m.Removes([]string{"a", "b"})
t.Assert(m.Map(), map[string]string{"c": "c"})
})
}
func Test_StrStrMap_Iterator(t *testing.T) {
expect := map[string]string{"a": "a", "b": "b"}
m := gmap.NewStrStrMapFrom(expect)
m.Iterator(func(k string, v string) bool {
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
expect := map[string]string{"a": "a", "b": "b"}
m := gmap.NewStrStrMapFrom(expect)
m.Iterator(func(k string, v string) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v string) bool {
i++
return true
})
m.Iterator(func(k string, v string) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k string, v string) bool {
i++
return true
})
m.Iterator(func(k string, v string) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_StrStrMap_Lock(t *testing.T) {
expect := map[string]string{"a": "a", "b": "b"}
gtest.C(t, func(t *gtest.T) {
expect := map[string]string{"a": "a", "b": "b"}
m := gmap.NewStrStrMapFrom(expect)
m.LockFunc(func(m map[string]string) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]string) {
t.Assert(m, expect)
m := gmap.NewStrStrMapFrom(expect)
m.LockFunc(func(m map[string]string) {
t.Assert(m, expect)
})
m.RLockFunc(func(m map[string]string) {
t.Assert(m, expect)
})
})
}
func Test_StrStrMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewStrStrMapFrom(map[string]string{"a": "a", "b": "b", "c": "c"})
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewStrStrMapFrom(map[string]string{"a": "a", "b": "b", "c": "c"})
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone := m.Clone()
m.Remove("a")
//修改原 map,clone 后的 map 不影响
t.AssertIN("a", m_clone.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
m_clone.Remove("b")
//修改clone map,原 map 不影响
t.AssertIN("b", m.Keys())
})
}
func Test_StrStrMap_Merge(t *testing.T) {
m1 := gmap.NewStrStrMap()
m2 := gmap.NewStrStrMap()
m1.Set("a", "a")
m2.Set("b", "b")
m1.Merge(m2)
t.Assert(m1.Map(), map[string]string{"a": "a", "b": "b"})
gtest.C(t, func(t *gtest.T) {
m1 := gmap.NewStrStrMap()
m2 := gmap.NewStrStrMap()
m1.Set("a", "a")
m2.Set("b", "b")
m1.Merge(m2)
t.Assert(m1.Map(), map[string]string{"a": "a", "b": "b"})
})
}
func Test_StrStrMap_Map(t *testing.T) {
m := gmap.NewStrStrMap()
m.Set("1", "1")
m.Set("2", "2")
t.Assert(m.Get("1"), "1")
t.Assert(m.Get("2"), "2")
data := m.Map()
t.Assert(data["1"], "1")
t.Assert(data["2"], "2")
data["3"] = "3"
t.Assert(m.Get("3"), "3")
m.Set("4", "4")
t.Assert(data["4"], "4")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrStrMap()
m.Set("1", "1")
m.Set("2", "2")
t.Assert(m.Get("1"), "1")
t.Assert(m.Get("2"), "2")
data := m.Map()
t.Assert(data["1"], "1")
t.Assert(data["2"], "2")
data["3"] = "3"
t.Assert(m.Get("3"), "3")
m.Set("4", "4")
t.Assert(data["4"], "4")
})
}
func Test_StrStrMap_MapCopy(t *testing.T) {
m := gmap.NewStrStrMap()
m.Set("1", "1")
m.Set("2", "2")
t.Assert(m.Get("1"), "1")
t.Assert(m.Get("2"), "2")
data := m.MapCopy()
t.Assert(data["1"], "1")
t.Assert(data["2"], "2")
data["3"] = "3"
t.Assert(m.Get("3"), "")
m.Set("4", "4")
t.Assert(data["4"], "")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrStrMap()
m.Set("1", "1")
m.Set("2", "2")
t.Assert(m.Get("1"), "1")
t.Assert(m.Get("2"), "2")
data := m.MapCopy()
t.Assert(data["1"], "1")
t.Assert(data["2"], "2")
data["3"] = "3"
t.Assert(m.Get("3"), "")
m.Set("4", "4")
t.Assert(data["4"], "")
})
}
func Test_StrStrMap_FilterEmpty(t *testing.T) {
m := gmap.NewStrStrMap()
m.Set("1", "")
m.Set("2", "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), "")
t.Assert(m.Get("2"), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), "2")
gtest.C(t, func(t *gtest.T) {
m := gmap.NewStrStrMap()
m.Set("1", "")
m.Set("2", "2")
t.Assert(m.Size(), 2)
t.Assert(m.Get("1"), "")
t.Assert(m.Get("2"), "2")
m.FilterEmpty()
t.Assert(m.Size(), 1)
t.Assert(m.Get("2"), "2")
})
}
func Test_StrStrMap_Json(t *testing.T) {
@ -272,37 +289,37 @@ func Test_StrStrMap_Pops(t *testing.T) {
}
func TestStrStrMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.StrStrMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":"v1","k2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": "v1",
"k2": "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
}

View File

@ -52,57 +52,65 @@ func Test_TreeMap_Basic(t *testing.T) {
})
}
func Test_TreeMap_Set_Fun(t *testing.T) {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.GetOrSetFunc("fun", getValue)
m.GetOrSetFuncLock("funlock", getValue)
t.Assert(m.Get("funlock"), 3)
t.Assert(m.Get("fun"), 3)
m.GetOrSetFunc("fun", getValue)
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
gtest.C(t, func(t *gtest.T) {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.GetOrSetFunc("fun", getValue)
m.GetOrSetFuncLock("funlock", getValue)
t.Assert(m.Get("funlock"), 3)
t.Assert(m.Get("fun"), 3)
m.GetOrSetFunc("fun", getValue)
t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
})
}
func Test_TreeMap_Batch(t *testing.T) {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
gtest.C(t, func(t *gtest.T) {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
})
}
func Test_TreeMap_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
gtest.C(t, func(t *gtest.T) {
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
t.Assert(i, 2)
t.Assert(j, 1)
}
func Test_TreeMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
})
}
func Test_TreeMap_Json(t *testing.T) {
@ -150,37 +158,37 @@ func Test_TreeMap_Json(t *testing.T) {
}
func TestTreeMap_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Map *gmap.TreeMap
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": []byte(`{"k1":"v1","k2":"v2"}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"map": g.Map{
"k1": "v1",
"k2": "v2",
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Map.Size(), 2)
t.Assert(t.Map.Get("k1"), "v1")
t.Assert(t.Map.Get("k2"), "v2")
t.Assert(v.Name, "john")
t.Assert(v.Map.Size(), 2)
t.Assert(v.Map.Get("k1"), "v1")
t.Assert(v.Map.Get("k2"), "v2")
})
}

View File

@ -17,41 +17,49 @@ import (
)
func TestQueue_Len(t *testing.T) {
max := 100
for n := 10; n < max; n++ {
q1 := gqueue.New(max)
for i := 0; i < max; i++ {
q1.Push(i)
gtest.C(t, func(t *gtest.T) {
max := 100
for n := 10; n < max; n++ {
q1 := gqueue.New(max)
for i := 0; i < max; i++ {
q1.Push(i)
}
t.Assert(q1.Len(), max)
t.Assert(q1.Size(), max)
}
t.Assert(q1.Len(), max)
t.Assert(q1.Size(), max)
}
})
}
func TestQueue_Basic(t *testing.T) {
q := gqueue.New()
for i := 0; i < 100; i++ {
q.Push(i)
}
t.Assert(q.Pop(), 0)
t.Assert(q.Pop(), 1)
gtest.C(t, func(t *gtest.T) {
q := gqueue.New()
for i := 0; i < 100; i++ {
q.Push(i)
}
t.Assert(q.Pop(), 0)
t.Assert(q.Pop(), 1)
})
}
func TestQueue_Pop(t *testing.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
q1.Push(3)
q1.Push(4)
i1 := q1.Pop()
t.Assert(i1, 1)
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
q1.Push(3)
q1.Push(4)
i1 := q1.Pop()
t.Assert(i1, 1)
})
}
func TestQueue_Close(t *testing.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})
}

View File

@ -365,38 +365,38 @@ func TestSet_AddIfNotExistFunc(t *testing.T) {
}
func TestSet_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Set *gset.Set
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"set": []byte(`["k1","k2","k3"]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains("k1"), true)
t.Assert(t.Set.Contains("k2"), true)
t.Assert(t.Set.Contains("k3"), true)
t.Assert(t.Set.Contains("k4"), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains("k1"), true)
t.Assert(v.Set.Contains("k2"), true)
t.Assert(v.Set.Contains("k3"), true)
t.Assert(v.Set.Contains("k4"), false)
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"set": g.Slice{"k1", "k2", "k3"},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains("k1"), true)
t.Assert(t.Set.Contains("k2"), true)
t.Assert(t.Set.Contains("k3"), true)
t.Assert(t.Set.Contains("k4"), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains("k1"), true)
t.Assert(v.Set.Contains("k2"), true)
t.Assert(v.Set.Contains("k3"), true)
t.Assert(v.Set.Contains("k4"), false)
})
}

View File

@ -328,38 +328,38 @@ func TestIntSet_AddIfNotExistFunc(t *testing.T) {
}
func TestIntSet_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Set *gset.IntSet
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(g.Map{
"name": "john",
"set": []byte(`[1,2,3]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains(1), true)
t.Assert(t.Set.Contains(2), true)
t.Assert(t.Set.Contains(3), true)
t.Assert(t.Set.Contains(4), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains(1), true)
t.Assert(v.Set.Contains(2), true)
t.Assert(v.Set.Contains(3), true)
t.Assert(v.Set.Contains(4), false)
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(g.Map{
"name": "john",
"set": g.Slice{1, 2, 3},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains(1), true)
t.Assert(t.Set.Contains(2), true)
t.Assert(t.Set.Contains(3), true)
t.Assert(t.Set.Contains(4), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains(1), true)
t.Assert(v.Set.Contains(2), true)
t.Assert(v.Set.Contains(3), true)
t.Assert(v.Set.Contains(4), false)
})
}

View File

@ -364,38 +364,38 @@ func TestStrSet_AddIfNotExistFunc(t *testing.T) {
}
func TestStrSet_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Set *gset.StrSet
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(g.Map{
"name": "john",
"set": []byte(`["1","2","3"]`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains("1"), true)
t.Assert(t.Set.Contains("2"), true)
t.Assert(t.Set.Contains("3"), true)
t.Assert(t.Set.Contains("4"), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains("1"), true)
t.Assert(v.Set.Contains("2"), true)
t.Assert(v.Set.Contains("3"), true)
t.Assert(v.Set.Contains("4"), false)
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(g.Map{
"name": "john",
"set": g.SliceStr{"1", "2", "3"},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Set.Size(), 3)
t.Assert(t.Set.Contains("1"), true)
t.Assert(t.Set.Contains("2"), true)
t.Assert(t.Set.Contains("3"), true)
t.Assert(t.Set.Contains("4"), false)
t.Assert(v.Name, "john")
t.Assert(v.Set.Size(), 3)
t.Assert(v.Set.Contains("1"), true)
t.Assert(v.Set.Contains("2"), true)
t.Assert(v.Set.Contains("3"), true)
t.Assert(v.Set.Contains("4"), false)
})
}

View File

@ -99,14 +99,17 @@ func Test_AVLTree_Get_Set_Var(t *testing.T) {
}
func Test_AVLTree_Batch(t *testing.T) {
m := gtree.NewAVLTree(gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
gtest.C(t, func(t *gtest.T) {
m := gtree.NewAVLTree(gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
})
}
func Test_AVLTree_Iterator(t *testing.T) {
keys := []string{"1", "key1", "key2", "key3", "key4"}
keyLen := len(keys)
index := 0
@ -114,18 +117,21 @@ func Test_AVLTree_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(k, keys[index])
index++
t.Assert(expect[k], v)
return true
})
m.IteratorDesc(func(k interface{}, v interface{}) bool {
index--
t.Assert(k, keys[index])
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(k, keys[index])
index++
t.Assert(expect[k], v)
return true
})
m.IteratorDesc(func(k interface{}, v interface{}) bool {
index--
t.Assert(k, keys[index])
t.Assert(expect[k], v)
return true
})
})
m.Print()
@ -197,16 +203,18 @@ func Test_AVLTree_IteratorFrom(t *testing.T) {
}
func Test_AVLTree_Clone(t *testing.T) {
//clone 方法是深克隆
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gtree.NewAVLTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
})
}
func Test_AVLTree_LRNode(t *testing.T) {

View File

@ -94,11 +94,13 @@ func Test_BTree_Get_Set_Var(t *testing.T) {
}
func Test_BTree_Batch(t *testing.T) {
m := gtree.NewBTree(3, gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
gtest.C(t, func(t *gtest.T) {
m := gtree.NewBTree(3, gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
})
}
func Test_BTree_Iterator(t *testing.T) {
@ -109,18 +111,21 @@ func Test_BTree_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{"key4": "val4", 1: 1, "key1": "val1", "key2": "val2", "key3": "val3"}
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, expect)
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(k, keys[index])
index++
t.Assert(expect[k], v)
return true
})
m.IteratorDesc(func(k interface{}, v interface{}) bool {
index--
t.Assert(k, keys[index])
t.Assert(expect[k], v)
return true
gtest.C(t, func(t *gtest.T) {
m.Iterator(func(k interface{}, v interface{}) bool {
t.Assert(k, keys[index])
index++
t.Assert(expect[k], v)
return true
})
m.IteratorDesc(func(k interface{}, v interface{}) bool {
index--
t.Assert(k, keys[index])
t.Assert(expect[k], v)
return true
})
})
m.Print()
@ -191,16 +196,18 @@ func Test_BTree_IteratorFrom(t *testing.T) {
}
func Test_BTree_Clone(t *testing.T) {
//clone 方法是深克隆
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
gtest.C(t, func(t *gtest.T) {
//clone 方法是深克隆
m := gtree.NewBTreeFrom(3, gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
t.AssertIN(1, m_clone.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
t.AssertIN("key1", m.Keys())
})
}
func Test_BTree_LRNode(t *testing.T) {

View File

@ -98,28 +98,28 @@ func Test_Bool_JSON(t *testing.T) {
}
func Test_Bool_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Bool
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "true",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), true)
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), true)
})
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "false",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), false)
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), false)
})
}

View File

@ -60,18 +60,18 @@ func Test_Byte_JSON(t *testing.T) {
}
func Test_Byte_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Byte
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "2",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "2")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "2")
})
}

View File

@ -46,18 +46,18 @@ func Test_Bytes_JSON(t *testing.T) {
}
func Test_Bytes_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Bytes
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -47,18 +47,18 @@ func Test_Float32_JSON(t *testing.T) {
}
func Test_Float32_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Float32
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123.456",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123.456")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123.456")
})
}

View File

@ -45,18 +45,18 @@ func Test_Float64_JSON(t *testing.T) {
}
func Test_Float64_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Float64
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123.456",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123.456")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123.456")
})
}

View File

@ -58,18 +58,18 @@ func Test_Int32_JSON(t *testing.T) {
}
func Test_Int32_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Int32
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -57,18 +57,18 @@ func Test_Int64_JSON(t *testing.T) {
}
func Test_Int64_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Int64
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -57,18 +57,18 @@ func Test_Int_JSON(t *testing.T) {
}
func Test_Int_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Int
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -16,12 +16,12 @@ import (
func Test_Interface(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t := Temp{Name: "gf", Age: 18}
t1 := Temp{Name: "gf", Age: 19}
i := gtype.New(t)
t1 := Temp{Name: "gf", Age: 18}
t2 := Temp{Name: "gf", Age: 19}
i := gtype.New(t1)
iClone := i.Clone()
t.AssertEQ(iClone.Set(t1), t)
t.AssertEQ(iClone.Val().(Temp), t1)
t.AssertEQ(iClone.Set(t2), t1)
t.AssertEQ(iClone.Val().(Temp), t2)
//空参测试
i1 := gtype.New()
@ -47,18 +47,18 @@ func Test_Interface_JSON(t *testing.T) {
}
func Test_Interface_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Var *gtype.Interface
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -45,18 +45,18 @@ func Test_String_JSON(t *testing.T) {
}
func Test_String_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Var *gtype.String
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -57,18 +57,18 @@ func Test_Uint32_JSON(t *testing.T) {
}
func Test_Uint32_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Var *gtype.Uint32
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -62,18 +62,18 @@ func Test_Uint64_JSON(t *testing.T) {
}
func Test_Uint64_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Var *gtype.Uint64
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -56,18 +56,18 @@ func Test_Uint_JSON(t *testing.T) {
}
func Test_Uint_UnmarshalValue(t *testing.T) {
type T struct {
type V struct {
Name string
Var *gtype.Uint
}
gtest.C(t, func(t *gtest.T) {
var t *T
var v *V
err := gconv.Struct(map[string]interface{}{
"name": "john",
"var": "123",
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Var.Val(), "123")
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}

View File

@ -195,11 +195,11 @@ func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
}
result, err := db.BatchInsert(name, array.Slice())
t.Assert(err, nil)
gtest.Assert(err, nil)
n, e := result.RowsAffected()
t.Assert(e, nil)
t.Assert(n, SIZE)
gtest.Assert(e, nil)
gtest.Assert(n, SIZE)
return
}

View File

@ -635,13 +635,13 @@ func Test_DB_Time(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
t := time.Now()
t1 := time.Now()
result, err := db.Insert(table, g.Map{
"id": 300,
"passport": "t300",
"password": "123456",
"nickname": "T300",
"create_time": &t,
"create_time": &t1,
})
if err != nil {
gtest.Error(err)
@ -662,11 +662,10 @@ func Test_DB_Time(t *testing.T) {
}
func Test_DB_ToJson(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
result, err := db.Table(table).Fields("*").Where("id =? ", 1).Select()
@ -737,11 +736,10 @@ func Test_DB_ToJson(t *testing.T) {
}
func Test_DB_ToXml(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
record, err := db.Table(table).Fields("*").Where("id = ?", 1).One()
@ -807,7 +805,7 @@ func Test_DB_ToStringMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := "1"
result, err := db.Table(table).Fields("*").Where("id = ?", 1).Select()
@ -843,7 +841,7 @@ func Test_DB_ToIntMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -876,11 +874,10 @@ func Test_DB_ToIntMap(t *testing.T) {
}
func Test_DB_ToUintMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -914,11 +911,10 @@ func Test_DB_ToUintMap(t *testing.T) {
}
func Test_DB_ToStringRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -953,11 +949,10 @@ func Test_DB_ToStringRecord(t *testing.T) {
}
func Test_DB_ToIntRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -991,11 +986,10 @@ func Test_DB_ToIntRecord(t *testing.T) {
}
func Test_DB_ToUintRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
t.Assert(err, nil)
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -1030,7 +1024,6 @@ func Test_DB_ToUintRecord(t *testing.T) {
func Test_DB_TableField(t *testing.T) {
name := "field_test"
dropTable(name)
defer dropTable(name)
_, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
@ -1069,7 +1062,7 @@ func Test_DB_TableField(t *testing.T) {
if err != nil {
gtest.Fatal(err)
} else {
t.Assert(n, 1)
gtest.Assert(n, 1)
}
result, err := db.Table(name).Fields("*").Where("field_int = ?", 2).Select()
@ -1077,7 +1070,7 @@ func Test_DB_TableField(t *testing.T) {
gtest.Fatal(err)
}
t.Assert(result[0], data)
gtest.Assert(result[0], data)
}
func Test_DB_Prefix(t *testing.T) {

View File

@ -1515,7 +1515,7 @@ func Test_Model_Offset(t *testing.T) {
t.Assert(err, nil)
t.Assert(len(result), 2)
t.Assert(result[0]["id"], 6)
t.Assert(result[1]["id"], 7)
tgit.Assert(result[1]["id"], 7)
}
func Test_Model_ForPage(t *testing.T) {

View File

@ -14,71 +14,75 @@ import (
)
func Test_BeEncodeAndBeDecode(t *testing.T) {
for k, v := range testData {
ve := gbinary.BeEncode(v)
ve1 := gbinary.BeEncodeByLength(len(ve), v)
gtest.C(t, func(t *gtest.T) {
for k, v := range testData {
ve := gbinary.BeEncode(v)
ve1 := gbinary.BeEncodeByLength(len(ve), v)
//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
switch v.(type) {
case int:
t.Assert(gbinary.BeDecodeToInt(ve), v)
t.Assert(gbinary.BeDecodeToInt(ve1), v)
case int8:
t.Assert(gbinary.BeDecodeToInt8(ve), v)
t.Assert(gbinary.BeDecodeToInt8(ve1), v)
case int16:
t.Assert(gbinary.BeDecodeToInt16(ve), v)
t.Assert(gbinary.BeDecodeToInt16(ve1), v)
case int32:
t.Assert(gbinary.BeDecodeToInt32(ve), v)
t.Assert(gbinary.BeDecodeToInt32(ve1), v)
case int64:
t.Assert(gbinary.BeDecodeToInt64(ve), v)
t.Assert(gbinary.BeDecodeToInt64(ve1), v)
case uint:
t.Assert(gbinary.BeDecodeToUint(ve), v)
t.Assert(gbinary.BeDecodeToUint(ve1), v)
case uint8:
t.Assert(gbinary.BeDecodeToUint8(ve), v)
t.Assert(gbinary.BeDecodeToUint8(ve1), v)
case uint16:
t.Assert(gbinary.BeDecodeToUint16(ve1), v)
t.Assert(gbinary.BeDecodeToUint16(ve), v)
case uint32:
t.Assert(gbinary.BeDecodeToUint32(ve1), v)
t.Assert(gbinary.BeDecodeToUint32(ve), v)
case uint64:
t.Assert(gbinary.BeDecodeToUint64(ve), v)
t.Assert(gbinary.BeDecodeToUint64(ve1), v)
case bool:
t.Assert(gbinary.BeDecodeToBool(ve), v)
t.Assert(gbinary.BeDecodeToBool(ve1), v)
case string:
t.Assert(gbinary.BeDecodeToString(ve), v)
t.Assert(gbinary.BeDecodeToString(ve1), v)
case float32:
t.Assert(gbinary.BeDecodeToFloat32(ve), v)
t.Assert(gbinary.BeDecodeToFloat32(ve1), v)
case float64:
t.Assert(gbinary.BeDecodeToFloat64(ve), v)
t.Assert(gbinary.BeDecodeToFloat64(ve1), v)
default:
if v == nil {
continue
//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
switch v.(type) {
case int:
t.Assert(gbinary.BeDecodeToInt(ve), v)
t.Assert(gbinary.BeDecodeToInt(ve1), v)
case int8:
t.Assert(gbinary.BeDecodeToInt8(ve), v)
t.Assert(gbinary.BeDecodeToInt8(ve1), v)
case int16:
t.Assert(gbinary.BeDecodeToInt16(ve), v)
t.Assert(gbinary.BeDecodeToInt16(ve1), v)
case int32:
t.Assert(gbinary.BeDecodeToInt32(ve), v)
t.Assert(gbinary.BeDecodeToInt32(ve1), v)
case int64:
t.Assert(gbinary.BeDecodeToInt64(ve), v)
t.Assert(gbinary.BeDecodeToInt64(ve1), v)
case uint:
t.Assert(gbinary.BeDecodeToUint(ve), v)
t.Assert(gbinary.BeDecodeToUint(ve1), v)
case uint8:
t.Assert(gbinary.BeDecodeToUint8(ve), v)
t.Assert(gbinary.BeDecodeToUint8(ve1), v)
case uint16:
t.Assert(gbinary.BeDecodeToUint16(ve1), v)
t.Assert(gbinary.BeDecodeToUint16(ve), v)
case uint32:
t.Assert(gbinary.BeDecodeToUint32(ve1), v)
t.Assert(gbinary.BeDecodeToUint32(ve), v)
case uint64:
t.Assert(gbinary.BeDecodeToUint64(ve), v)
t.Assert(gbinary.BeDecodeToUint64(ve1), v)
case bool:
t.Assert(gbinary.BeDecodeToBool(ve), v)
t.Assert(gbinary.BeDecodeToBool(ve1), v)
case string:
t.Assert(gbinary.BeDecodeToString(ve), v)
t.Assert(gbinary.BeDecodeToString(ve1), v)
case float32:
t.Assert(gbinary.BeDecodeToFloat32(ve), v)
t.Assert(gbinary.BeDecodeToFloat32(ve1), v)
case float64:
t.Assert(gbinary.BeDecodeToFloat64(ve), v)
t.Assert(gbinary.BeDecodeToFloat64(ve1), v)
default:
if v == nil {
continue
}
res := make([]byte, len(ve))
err := gbinary.BeDecode(ve, res)
if err != nil {
t.Errorf("test data: %s, %v, error:%v", k, v, err)
}
t.Assert(res, v)
}
res := make([]byte, len(ve))
err := gbinary.BeDecode(ve, res)
if err != nil {
t.Errorf("test data: %s, %v, error:%v", k, v, err)
}
t.Assert(res, v)
}
}
})
}
func Test_BeEncodeStruct(t *testing.T) {
user := User{"wenzi1", 999, "www.baidu.com"}
ve := gbinary.BeEncode(user)
s := gbinary.BeDecodeToString(ve)
t.Assert(string(s), s)
gtest.C(t, func(t *gtest.T) {
user := User{"wenzi1", 999, "www.baidu.com"}
ve := gbinary.BeEncode(user)
s := gbinary.BeDecodeToString(ve)
t.Assert(string(s), s)
})
}

View File

@ -13,21 +13,27 @@ import (
)
func TestStripTags(t *testing.T) {
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
dst := `Test paragraph. Other text`
t.Assert(ghtml.StripTags(src), dst)
gtest.C(t, func(t *gtest.T) {
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
dst := `Test paragraph. Other text`
t.Assert(ghtml.StripTags(src), dst)
})
}
func TestEntities(t *testing.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
t.Assert(ghtml.Entities(src), dst)
t.Assert(ghtml.EntitiesDecode(dst), src)
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
t.Assert(ghtml.Entities(src), dst)
t.Assert(ghtml.EntitiesDecode(dst), src)
})
}
func TestSpecialChars(t *testing.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
t.Assert(ghtml.SpecialChars(src), dst)
t.Assert(ghtml.SpecialCharsDecode(dst), src)
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
t.Assert(ghtml.SpecialChars(src), dst)
t.Assert(ghtml.SpecialCharsDecode(dst), src)
})
}

View File

@ -31,28 +31,28 @@ func TestJson_UnmarshalJSON(t *testing.T) {
}
func TestJson_UnmarshalValue(t *testing.T) {
type T struct {
type Var struct {
Name string
Json *gjson.Json
}
// JSON
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"json": []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`),
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Json.Get("n"), "123456789")
t.Assert(t.Json.Get("m"), g.Map{"k": "v"})
t.Assert(t.Json.Get("m.k"), "v")
t.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
t.Assert(t.Json.Get("a.1"), 2)
t.Assert(v.Name, "john")
t.Assert(v.Json.Get("n"), "123456789")
t.Assert(v.Json.Get("m"), g.Map{"k": "v"})
t.Assert(v.Json.Get("m.k"), "v")
t.Assert(v.Json.Get("a"), g.Slice{1, 2, 3})
t.Assert(v.Json.Get("a.1"), 2)
})
// Map
gtest.C(t, func(t *gtest.T) {
var t *T
var v *Var
err := gconv.Struct(g.Map{
"name": "john",
"json": g.Map{
@ -60,13 +60,13 @@ func TestJson_UnmarshalValue(t *testing.T) {
"m": g.Map{"k": "v"},
"a": g.Slice{1, 2, 3},
},
}, &t)
}, &v)
t.Assert(err, nil)
t.Assert(t.Name, "john")
t.Assert(t.Json.Get("n"), "123456789")
t.Assert(t.Json.Get("m"), g.Map{"k": "v"})
t.Assert(t.Json.Get("m.k"), "v")
t.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
t.Assert(t.Json.Get("a.1"), 2)
t.Assert(v.Name, "john")
t.Assert(v.Json.Get("n"), "123456789")
t.Assert(v.Json.Get("m"), g.Map{"k": "v"})
t.Assert(v.Json.Get("m.k"), "v")
t.Assert(v.Json.Get("a"), g.Slice{1, 2, 3})
t.Assert(v.Json.Get("a.1"), 2)
})
}

View File

@ -88,116 +88,112 @@ func Test_Config2(t *testing.T) {
func Test_Config3(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "test.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
t.Assert(err, nil)
name := "test.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
t.Assert(err, nil)
err = gins.Config("test").AddPath(dirPath)
t.Assert(err, nil)
err = gins.Config("test").AddPath(dirPath)
t.Assert(err, nil)
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
// for gfsnotify callbacks to refresh cache of config file
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "config/test.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
t.Assert(err, nil)
err = gins.Config("test").AddPath(dirPath)
t.Assert(err, nil)
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
// for gfsnotify callbacks to refresh cache of config file for next unit testing case.
time.Sleep(500 * time.Millisecond)
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
// for gfsnotify callbacks to refresh cache of config file
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "config/test.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), configContent)
t.Assert(err, nil)
err = gins.Config("test").AddPath(dirPath)
t.Assert(err, nil)
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
// for gfsnotify callbacks to refresh cache of config file for next unit testing case.
time.Sleep(500 * time.Millisecond)
}
func Test_Config4(t *testing.T) {
// absolute path
gtest.C(t, func(t *gtest.T) {
// absolute path
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
t.Assert(gins.Config().AddPath(path), nil)
t.Assert(gins.Config().Get("test"), "v=1")
t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
t.Assert(gins.Config().AddPath(path), nil)
t.Assert(gins.Config().Get("test"), "v=1")
t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
t.Assert(gins.Config().AddPath(path), nil)
t.Assert(gins.Config().Get("test"), "v=1")
t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
t.Assert(gins.Config().AddPath(path), nil)
t.Assert(gins.Config().Get("test"), "v=1")
t.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").AddPath(path), nil)
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config("test").Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").AddPath(path), nil)
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
time.Sleep(500 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").AddPath(path), nil)
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
gtest.C(t, func(t *gtest.T) {
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.TimestampNano())
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
err := gfile.PutContents(file, configContent)
t.Assert(err, nil)
defer gfile.Remove(file)
defer gins.Config().Clear()
gins.Config("test").SetFileName("test.toml")
t.Assert(gins.Config("test").AddPath(path), nil)
t.Assert(gins.Config("test").Get("test"), "v=1")
t.Assert(gins.Config("test").Get("database.default.1.host"), "127.0.0.1")
t.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
}
func Test_Basic2(t *testing.T) {

View File

@ -20,18 +20,20 @@ import (
func Test_Database(t *testing.T) {
databaseContent := gfile.GetContents(gfile.Join(gdebug.TestDataPath(), "database", "config.toml"))
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent)
t.Assert(err, nil)
name := "config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent)
t.Assert(err, nil)
err = gins.Config().AddPath(dirPath)
t.Assert(err, nil)
err = gins.Config().AddPath(dirPath)
t.Assert(err, nil)
})
defer gins.Config().Clear()

View File

@ -20,18 +20,20 @@ import (
func Test_Redis(t *testing.T) {
redisContent := gfile.GetContents(gfile.Join(gdebug.TestDataPath(), "redis", "config.toml"))
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), redisContent)
t.Assert(err, nil)
name := "config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), redisContent)
t.Assert(err, nil)
err = gins.Config().AddPath(dirPath)
t.Assert(err, nil)
err = gins.Config().AddPath(dirPath)
t.Assert(err, nil)
})
defer gins.Config().Clear()

View File

@ -28,48 +28,48 @@ import (
func Test_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t := gi18n.New(gi18n.Options{
i18n := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n",
})
t.SetLanguage("none")
t.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
t.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
t.Assert(t.T("{#hello}{#world}"), "你好世界")
t.SetDelimiters("{$", "}")
t.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
t.Assert(t.T("{$hello}{$world}"), "你好世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
i18n.SetDelimiters("{$", "}")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
t.Assert(i18n.T("{$hello}{$world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
t := gi18n.New(gi18n.Options{
i18n := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-file",
})
t.SetLanguage("none")
t.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
t.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
t.Assert(t.T("{#hello}{#world}"), "你好世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
})
gtest.C(t, func(t *gtest.T) {
t := gi18n.New(gi18n.Options{
i18n := gi18n.New(gi18n.Options{
Path: gdebug.CallerDirectory() + gfile.Separator + "testdata" + gfile.Separator + "i18n-dir",
})
t.SetLanguage("none")
t.Assert(t.T("{#hello}{#world}"), "{#hello}{#world}")
i18n.SetLanguage("none")
t.Assert(i18n.T("{#hello}{#world}"), "{#hello}{#world}")
t.SetLanguage("ja")
t.Assert(t.T("{#hello}{#world}"), "こんにちは世界")
i18n.SetLanguage("ja")
t.Assert(i18n.T("{#hello}{#world}"), "こんにちは世界")
t.SetLanguage("zh-CN")
t.Assert(t.T("{#hello}{#world}"), "你好世界")
i18n.SetLanguage("zh-CN")
t.Assert(i18n.T("{#hello}{#world}"), "你好世界")
})
}

View File

@ -51,9 +51,9 @@ func Test_WebSocket(t *testing.T) {
err = conn.WriteMessage(websocket.TextMessage, msg)
t.Assert(err, nil)
t, data, err := conn.ReadMessage()
mt, data, err := conn.ReadMessage()
t.Assert(err, nil)
t.Assert(t, websocket.TextMessage)
t.Assert(mt, websocket.TextMessage)
t.Assert(data, msg)
})
}

View File

@ -18,23 +18,22 @@ import (
func TestCron_Entry_Operations(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gtest.C(t, func(t *gtest.T) {
cron := gcron.New()
array := garray.New(true)
cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() {
glog.Println("add times")
array.Append(1)
})
t.Assert(cron.Size(), 0)
time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
time.Sleep(3000 * time.Millisecond)
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
cron := gcron.New()
array := garray.New(true)
cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() {
glog.Println("add times")
array.Append(1)
})
t.Assert(cron.Size(), 0)
time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
time.Sleep(3000 * time.Millisecond)
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
})
gtest.C(t, func(t *gtest.T) {
cron := gcron.New()
array := garray.New(true)
entry, err1 := cron.Add("* * * * * *", func() {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,9 @@ import (
func Test_StorageRedisHashTable(t *testing.T) {
redis, err := gredis.NewFromStr("127.0.0.1:6379,0")
t.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
t.Assert(err, nil)
})
storage := gsession.NewStorageRedisHashTable(redis)
manager := gsession.New(time.Second, storage)
@ -78,7 +80,9 @@ func Test_StorageRedisHashTable(t *testing.T) {
func Test_StorageRedisHashTablePrefix(t *testing.T) {
redis, err := gredis.NewFromStr("127.0.0.1:6379,0")
t.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
t.Assert(err, nil)
})
prefix := "s_"
storage := gsession.NewStorageRedisHashTable(redis, prefix)

View File

@ -18,7 +18,7 @@ import (
func Test_StorageRedis(t *testing.T) {
redis, err := gredis.NewFromStr("127.0.0.1:6379,0")
t.Assert(err, nil)
gtest.Assert(err, nil)
storage := gsession.NewStorageRedis(redis)
manager := gsession.New(time.Second, storage)
@ -79,7 +79,7 @@ func Test_StorageRedis(t *testing.T) {
func Test_StorageRedisPrefix(t *testing.T) {
redis, err := gredis.NewFromStr("127.0.0.1:6379,0")
t.Assert(err, nil)
gtest.Assert(err, nil)
prefix := "s_"
storage := gsession.NewStorageRedis(redis, prefix)

View File

@ -19,10 +19,10 @@ func Test_Json_Pointer(t *testing.T) {
type T struct {
Time *gtime.Time
}
t := new(T)
t1 := new(T)
s := "2006-01-02 15:04:05"
t.Time = gtime.NewFromStr(s)
j, err := json.Marshal(t)
t1.Time = gtime.NewFromStr(s)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{"Time":"2006-01-02 15:04:05"}`)
})
@ -31,8 +31,8 @@ func Test_Json_Pointer(t *testing.T) {
type T struct {
Time *gtime.Time
}
t := new(T)
j, err := json.Marshal(t)
t1 := new(T)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{"Time":null}`)
})
@ -41,18 +41,18 @@ func Test_Json_Pointer(t *testing.T) {
type T struct {
Time *gtime.Time `json:"time,omitempty"`
}
t := new(T)
j, err := json.Marshal(t)
t1 := new(T)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{}`)
})
// Unmarshal
gtest.C(t, func(t *gtest.T) {
var t gtime.Time
var t1 gtime.Time
s := []byte(`"2006-01-02 15:04:05"`)
err := json.Unmarshal(s, &t)
err := json.Unmarshal(s, &t1)
t.Assert(err, nil)
t.Assert(t.String(), "2006-01-02 15:04:05")
t.Assert(t1.String(), "2006-01-02 15:04:05")
})
}
@ -62,10 +62,10 @@ func Test_Json_Struct(t *testing.T) {
type T struct {
Time gtime.Time
}
t := new(T)
t1 := new(T)
s := "2006-01-02 15:04:05"
t.Time = *gtime.NewFromStr(s)
j, err := json.Marshal(t)
t1.Time = *gtime.NewFromStr(s)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{"Time":"2006-01-02 15:04:05"}`)
})
@ -74,8 +74,8 @@ func Test_Json_Struct(t *testing.T) {
type T struct {
Time gtime.Time
}
t := new(T)
j, err := json.Marshal(t)
t1 := new(T)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{"Time":""}`)
})
@ -84,8 +84,8 @@ func Test_Json_Struct(t *testing.T) {
type T struct {
Time gtime.Time `json:"time,omitempty"`
}
t := new(T)
j, err := json.Marshal(t)
t1 := new(T)
j, err := json.Marshal(t1)
t.Assert(err, nil)
t.Assert(j, `{"time":""}`)
})

View File

@ -28,12 +28,12 @@ func Test_New(t *testing.T) {
func Test_Nil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var t *gtime.Time
t.Assert(t.String(), "")
var t1 *gtime.Time
t.Assert(t1.String(), "")
})
gtest.C(t, func(t *gtest.T) {
var t gtime.Time
t.Assert(t.String(), "")
var t1 gtime.Time
t.Assert(t1.String(), "")
})
}

View File

@ -18,60 +18,68 @@ import (
)
func TestEntry_Start_Stop_Close(t *testing.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
entry.Stop()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
entry.Start()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 2)
entry.Close()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 2)
gtest.C(t, func(t *gtest.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
entry.Stop()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
entry.Start()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 2)
entry.Close()
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 2)
t.Assert(entry.Status(), gtimer.STATUS_CLOSED)
t.Assert(entry.Status(), gtimer.STATUS_CLOSED)
})
}
func TestEntry_Singleton(t *testing.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
time.Sleep(10 * time.Second)
})
t.Assert(entry.IsSingleton(), false)
entry.SetSingleton(true)
t.Assert(entry.IsSingleton(), true)
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
gtest.C(t, func(t *gtest.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
time.Sleep(10 * time.Second)
})
t.Assert(entry.IsSingleton(), false)
entry.SetSingleton(true)
t.Assert(entry.IsSingleton(), true)
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
})
}
func TestEntry_SetTimes(t *testing.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
gtest.C(t, func(t *gtest.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(200*time.Millisecond, func() {
array.Append(1)
})
entry.SetTimes(2)
time.Sleep(1200 * time.Millisecond)
t.Assert(array.Len(), 2)
})
entry.SetTimes(2)
time.Sleep(1200 * time.Millisecond)
t.Assert(array.Len(), 2)
}
func TestEntry_Run(t *testing.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(1000*time.Millisecond, func() {
array.Append(1)
gtest.C(t, func(t *gtest.T) {
timer := New()
array := garray.New(true)
entry := timer.Add(1000*time.Millisecond, func() {
array.Append(1)
})
entry.Run()
t.Assert(array.Len(), 1)
})
entry.Run()
t.Assert(array.Len(), 1)
}

View File

@ -12,7 +12,7 @@ import (
// T is the testing unit case management object.
type T struct {
T *testing.T
*testing.T
}
// Assert checks <value> and <expect> EQUAL.

View File

@ -18,20 +18,18 @@ import (
)
func Test_Parse(t *testing.T) {
// url
gtest.C(t, func(t *gtest.T) {
// url
gtest.C(t, func(t *gtest.T) {
s := "goframe.org/index?name=john&score=100"
u, err := url.Parse(s)
t.Assert(err, nil)
m, err := gstr.Parse(u.RawQuery)
t.Assert(err, nil)
t.Assert(m["name"], "john")
t.Assert(m["score"], "100")
})
s := "goframe.org/index?name=john&score=100"
u, err := url.Parse(s)
t.Assert(err, nil)
m, err := gstr.Parse(u.RawQuery)
t.Assert(err, nil)
t.Assert(m["name"], "john")
t.Assert(m["score"], "100")
// name overwrite
m, err := gstr.Parse("a=1&a=2")
m, err = gstr.Parse("a=1&a=2")
t.Assert(err, nil)
t.Assert(m, g.Map{
"a": 2,

View File

@ -15,23 +15,27 @@ import (
)
func Test_Map(t *testing.T) {
rule := "ipv4"
val := "0.0.0"
msg := map[string]string{
"ipv4": "IPv4地址格式不正确",
}
gtest.C(t, func(t *gtest.T) {
rule := "ipv4"
val := "0.0.0"
msg := map[string]string{
"ipv4": "IPv4地址格式不正确",
}
err := gvalid.Check(val, rule, nil)
t.Assert(err.Map(), msg)
err := gvalid.Check(val, rule, nil)
t.Assert(err.Map(), msg)
})
}
func Test_FirstString(t *testing.T) {
rule := "ipv4"
val := "0.0.0"
gtest.C(t, func(t *gtest.T) {
rule := "ipv4"
val := "0.0.0"
err := gvalid.Check(val, rule, nil)
n := err.FirstString()
t.Assert(n, "IPv4地址格式不正确")
err := gvalid.Check(val, rule, nil)
n := err.FirstString()
t.Assert(n, "IPv4地址格式不正确")
})
}
func Test_SetDefaultErrorMsgs(t *testing.T) {