示例#1
0
func TestIndexDistance(t *testing.T) {
	tt := testing2.Wrap(t)
	index, indexUsed, remains := SegmentIndex([]int{2, 3}, 6)
	tt.Eq(-1, index)
	tt.Eq(0, indexUsed)
	tt.Eq(-1, remains)
}
示例#2
0
func TestSearch(t *testing.T) {
	tt := testing2.Wrap(t)

	strings := []string{"a", "b", "c", "d", "e"}
	tt.Eq(1, Search(strings, "b", 0))
	tt.Eq(-1, Search(strings, "b", 1))
}
示例#3
0
文件: kv_test.go 项目: cosiner/kv
func TestRedis(t *testing.T) {
	tt := testing2.Wrap(t)

	r := NewRedis(os2.EnvDef("WERCKER_REDIS_HOST", "localhost")+":6379", nil)

	tt.Nil(r.Set("User1", EncVal(User{
		Id:   "1",
		Name: "User1",
	})))
	tt.Nil(r.Set("User2", EncVal(User{
		Id:   "2",
		Name: "User2",
	})))

	u1 := User{}
	tt.Nil(r.DecodeGet("User1", &u1))
	tt.Eq("1", u1.Id)
	tt.Eq("User1", u1.Name)

	u2 := User{}
	tt.Nil(r.DecodeGet("User2", &u2))
	tt.Eq("2", u2.Id)
	tt.Eq("User2", u2.Name)

	c, err := r.Del("User1", "User2")
	tt.Eq(2, c)
	tt.Nil(err)
}
示例#4
0
func TestMonthDays(t *testing.T) {
	tt := testing2.Wrap(t)
	type YearMonth struct {
		Year, Month int
		Days        int
		IsLeap      bool
	}

	tests := []YearMonth{
		YearMonth{2000, 3, 31, true},
		YearMonth{2000, 4, 30, true},
		YearMonth{2000, 2, 29, true},
		YearMonth{2001, 2, 28, false},
		YearMonth{2300, 2, 28, false},
		YearMonth{2400, 2, 29, true},
	}

	for _, t := range tests {
		tt.Eq(t.Days, MonthDays(t.Year, t.Month))
		tt.Eq(t.IsLeap, IsLeapYear(t.Year))
	}

	defer tt.Recover()
	MonthDays(2014, 13)
}
示例#5
0
func TestConflictWildcardCatchall(t *testing.T) {
	tt := testing2.Wrap(t)
	rt := new(router)
	tt.True(rt.HandleFunc("/:user/:id", GET, EmptyHandlerFunc) == nil)
	e := rt.HandleFunc("/*user", GET, EmptyHandlerFunc)
	tt.True(e != nil)
}
示例#6
0
func TestFilter(t *testing.T) {
	tt := testing2.Wrap(t)
	s := NewServer()

	var n int
	ft := func(req Request, resp Response, chain FilterChain) {
		n++
		chain(req, resp)
	}

	s.Handle("/", ft)
	s.Handle("/user", ft)
	s.Handle("/user/info", ft)

	s.Get("/user/info/aaa", Intercept(func(Request, Response) {
		n++
	}, ft, ft, ft))

	s.ResMaster.DefUse(resource.RES_JSON, resource.JSON{})

	s.ServeHTTP(NewMockWriter(os.Stdout), &http.Request{
		Method: "Get",
		URL: &url.URL{
			Path: "/user/info/aaa",
		},
	})

	tt.Eq(7, n)
}
示例#7
0
func TestUnmarshalPrimitive(t *testing.T) {
	tt := testing2.Wrap(t)
	bs := "tzz"
	var b bool
	tt.True(UnmarshalPrimitive(bs, reflect.ValueOf(&b)) == nil)
	tt.True(b)
}
示例#8
0
func TestMarshalStruct(t *testing.T) {
	tt := testing2.Wrap(t)

	st := struct {
		Name string `fd:"nm"`
		Age  int    `fd:"Age"`
	}{
		"aaa",
		123,
	}
	mp := make(map[string]string)
	MarshalStruct(&st, mp, "fd")

	tt.DeepEq(map[string]string{
		"nm":  "aaa",
		"Age": "123",
	}, mp)

	mp["nm"] = "bbb"
	mp["age"] = "234"
	UnmarshalStruct(&st, StringMap(mp), "fd")

	tt.Eq("bbb", st.Name)
	tt.Eq(123, st.Age)
}
示例#9
0
func TestStrings(t *testing.T) {
	tt := testing2.Wrap(t)

	slice := Strings{}
	strings := Strings{"1", "2", "3", "4", "5", "6", "7", "8"}
	for _, s := range strings {
		slice = slice.IncrAppend(s)
		tt.Eq(len(slice), cap(slice))
	}
	slice = slice.FitCapToLen()
	tt.Eq(len(slice), cap(slice))

	slice = append(slice, "9", "10")
	tt.NE(len(slice), cap(slice))
	slice = slice.FitCapToLen()
	tt.Eq(len(slice), cap(slice))

	slice = slice.Remove(0)
	tt.DeepEq(Strings{"2", "3", "4", "5", "6", "7", "8", "9", "10"}, slice)
	slice = slice.Remove(slice.Len() - 1)
	tt.DeepEq(Strings{"2", "3", "4", "5", "6", "7", "8", "9"}, slice.Append("2").RmDups())

	tt.Eq("23456789", slice.Join("", ""))
	tt.Eq("2=?, 3=?, 4=?, 5=?, 6=?, 7=?, 8=?, 9=?", slice.Join("=?", ", "))
}
示例#10
0
func TestFormat(t *testing.T) {
	tt := testing2.Wrap(t)
	code := `package main

        func TestExport(t *testing.T) {
            tt := testing2.Wrap(t)
        tt.True(IsExported("Name"))
                tt.True(ToSameExported("Abcd", "abc") == "Abc")
tt.True(ToSameExported("dbcd", "Abc") == "abc");tt.True(ToExported("aaa") == "Aaa")
    tt.True(ToExported("") == "")
    tt.True(ToUnexported("Aaa") == "aaa")
    tt.True(ToUnexported("") == "")
}
    `
	out := bytes.NewBuffer(make([]byte, 0, 1024))
	tt.True(Format("test", strings.NewReader(code), out) == nil)

	tt.Eq(strings2.RemoveSpace(`package main

func TestExport(t *testing.T) {
        tt := testing2.Wrap(t)
        tt.True(IsExported("Name"))
        tt.True(ToSameExported("Abcd", "abc") == "Abc")
        tt.True(ToSameExported("dbcd", "Abc") == "abc")
        tt.True(ToExported("aaa") == "Aaa")
        tt.True(ToExported("") == "")
        tt.True(ToUnexported("Aaa") == "aaa")
        tt.True(ToUnexported("") == "")
}
`), strings2.RemoveSpace(out.String()))
}
示例#11
0
func TestCond(t *testing.T) {
	tt := testing2.Wrap(t)
	tt.True(Cond(true).String("1", "2") == "1")
	tt.True(Cond(true).Int(1, 2) == 1)
	tt.True(Cond(true).Int8(1, 2) == 1)
	tt.True(Cond(true).Int16(1, 2) == 1)
	tt.True(Cond(true).Int32(1, 2) == 1)
	tt.True(Cond(true).Int64(1, 2) == 1)
	tt.True(Cond(true).Uint(1, 2) == 1)
	tt.True(Cond(true).Uint8(1, 2) == 1)
	tt.True(Cond(true).Uint16(1, 2) == 1)
	tt.True(Cond(true).Uint32(1, 2) == 1)
	tt.True(Cond(true).Uint64(1, 2) == 1)

	tt.True(Cond(false).String("1", "2") == "2")
	tt.True(Cond(false).Int(1, 2) == 2)
	tt.True(Cond(false).Int8(1, 2) == 2)
	tt.True(Cond(false).Int16(1, 2) == 2)
	tt.True(Cond(false).Int32(1, 2) == 2)
	tt.True(Cond(false).Int64(1, 2) == 2)
	tt.True(Cond(false).Uint(1, 2) == 2)
	tt.True(Cond(false).Uint8(1, 2) == 2)
	tt.True(Cond(false).Uint16(1, 2) == 2)
	tt.True(Cond(false).Uint32(1, 2) == 2)
	tt.True(Cond(false).Uint64(1, 2) == 2)
}
示例#12
0
func TestReverseBits(t *testing.T) {
	tt := testing2.Wrap(t)
	// 0000 0   0001 1  0010 2  0100 4  1000 8
	//          0011 3  0110 6  1100 C  1001 9
	tt.Eq(uint(0xcccccccc), ReverseBits(0x3333333300000000))
	tt.Eq(uint(0x48484848), ReverseBits(0x1212121200000000))
}
示例#13
0
func TestPrefix(t *testing.T) {
	tt := testing2.Wrap(t)

	tree := Trie{}

	tree.AddPath("1234", 1)
	tree.AddPath("234", 2)
	tree.AddPath("12", 3)
	tree.AddPath("347", 4)
	tree.AddPath("00", 5)

	tt.Nil(tree.PrefixMatchValue(""))
	tt.Nil(tree.PrefixMatchValue("1"))
	tt.Nil(tree.PrefixMatchValue("2"))
	tt.Nil(tree.PrefixMatchValue("3"))
	tt.Nil(tree.PrefixMatchValue("0"))
	tt.Nil(tree.PrefixMatchValue("13"))
	tt.Nil(tree.PrefixMatchValue("01"))

	tt.Eq(1, tree.PrefixMatchValue("1234").(int))
	tt.Eq(1, tree.PrefixMatchValue("12345").(int))

	tt.Eq(3, tree.PrefixMatchValue("12").(int))
	tt.Eq(3, tree.PrefixMatchValue("123").(int))
	tt.Eq(3, tree.PrefixMatchValue("124").(int))
}
示例#14
0
func TestFunc(t *testing.T) {
	tt := testing2.Wrap(t)
	type Test struct {
		Time  int64
		Human string
	}

	tests := []Test{
		Test{0, "0ns"},
		Test{999, "999ns"},

		Test{1000, "1us"},
		Test{1499, "1us"},
		Test{1500, "2us"},

		Test{1000 * 1000, "1ms"},
		Test{1000 * 1499, "1ms"},
		Test{1000 * 1500, "2ms"},

		Test{1000 * 1000 * 1000, "1s"},
		Test{1000 * 1000 * 1499, "1s"},
		Test{1000 * 1000 * 1500, "2s"},

		Test{1000 * 1000 * 1000 * 10000, "10000s"},
	}

	for _, test := range tests {
		tt.Eq(test.Human, ToHuman(test.Time))
	}
}
示例#15
0
func TestBinaryTree(t *testing.T) {
	tt := testing2.Wrap(t)

	bt := Binary{}
	bt.Add(1, 'A', false)
	bt.Add(2, 'B', false)
	bt.Add(3, 'C', false)
	bt.Add(4, 'D', false)
	bt.Add(5, 'E', false)
	bt.Add(6, 'F', false)
	bt.Add(7, 'G', false)

	tt.Eq('A', bt.Search(1).(int32))
	tt.Eq('B', bt.Search(2).(int32))
	tt.Eq('C', bt.Search(3).(int32))
	tt.Eq('D', bt.Search(4).(int32))
	tt.Eq('E', bt.Search(5).(int32))
	tt.Eq('F', bt.Search(6).(int32))
	tt.Eq('G', bt.Search(7).(int32))

	tt.Nil(bt.Search(8))
	tt.Nil(bt.Search(9))

	bt.Add(7, 'H', true)
	tt.Eq('H', bt.Search(7).(int32))

	bt.Add(7, 'I', false)
	tt.Eq('H', bt.Search(7).(int32))
}
示例#16
0
func TestRoutinePool(t *testing.T) {
	tt := testing2.Wrap(t)

	var done int64
	var jobs int64
	pool := New(func(job Job) {
		time.Sleep(1 * time.Millisecond)
		atomic.AddInt64(&done, 1)

	}, 20, 20, 0)
	for i := 0; i < 10; i++ {
		go func(i int) {
			for j := 0; j < 1000; j++ {
				atomic.AddInt64(&jobs, 1)
				tt.True(pool.Do(i*10 + j))
			}
		}(i)
	}

	time.Sleep(2 * time.Second)
	pool.Close()
	time.Sleep(2 * time.Second)
	t.Log(pool.Info())
	t.Log(atomic.LoadInt64(&jobs) - atomic.LoadInt64(&done))

	tt.False(pool.Do(123))
}
示例#17
0
func TestPair(t *testing.T) {
	tt := testing2.Wrap(t)
	p := Parse("aa=false", "=")
	tt.True(p.HasKey())
	v, e := p.BoolValue()
	tt.True(e == nil)
	tt.False(v)
}
示例#18
0
func TestValidateM(t *testing.T) {
	tt := testing2.Wrap(t)

	err := errors.Err("incorrect length")
	v := ValidLength(3, 10, err).ValidateM
	tt.Eq(err, v("a23", "a"))
	tt.Nil(v("a23", "1234"))
}
示例#19
0
func TestRparse(t *testing.T) {
	tt := testing2.Wrap(t)
	p := Rparse("aa=bb=1", "=")

	val, err := p.IntValue()
	tt.Nil(err)
	tt.Eq(1, val)
}
示例#20
0
func TestPath(t *testing.T) {
	tt := testing2.Wrap(t)
	out := bytes.NewBuffer(make([]byte, 0, 1024))
	WriteImportpath(out, "github.com", "cosiner", "gohper")
	tt.Eq(`"github.com/cosiner/gohper"`+"\n", out.String())

	PackagePath("bufio") // difficult to test
}
示例#21
0
func TestCompile(t *testing.T) {
	tt := testing2.Wrap(t)
	tt.Log(len(strings.Split("", "/")))
	tt.Log(compile("/"))
	tt.Log(compile("/:user/:id/:a"))
	tt.Log(compile("/user/:a/:abc/"))
	tt.Log(compile("/user/:/:abc/"))
}
示例#22
0
func TestError(t *testing.T) {
	tt := testing2.Wrap(t)
	err := errors.Err(`Duplicate entry '14d1b6c34a001-1648e0754a001' for key 'PRIMARY'`) // for combined primary key
	tt.Eq(PRIMARY_KEY, duplicateKey(err))

	err = errors.Err("CONSTRAINT `article_vote_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `article` (`article_id`)")
	tt.Eq("article_id", foreignKey(err))
}
示例#23
0
func TestBitSet(t *testing.T) {
	tt := testing2.Wrap(t)
	list := []uint{1, 2, 4, 5, 6, 7, 60}
	s := NewBitset(32, 1)
	tt.True(s.UnitCount() == 1)
	tt.True(s.Uint() == 2)
	tt.True(s.Uint64() == 2)
	testBitset(tt, s, list)
}
示例#24
0
func TestRemoveElement(t *testing.T) {
	tt := testing2.Wrap(t)
	eles := []interface{}{1, 2, 3, 4, 5, 6}
	tt.DeepEq([]interface{}{1, 3, 4, 5, 6}, RemoveElement(eles, 1))
	eles = []interface{}{1, 2, 3, 4, 5, 6}
	tt.DeepEq([]interface{}{1, 2, 3, 4, 6}, RemoveElement(eles, 4))
	eles = []interface{}{1, 2, 3, 4, 5, 6}
	tt.DeepEq([]interface{}{1, 2, 3, 4, 5, 6}, RemoveElement(eles, 6))
}
示例#25
0
func TestUser(t *testing.T) {
	tt := testing2.Wrap(t)

	u1 := User{
		Name: "User1",
		Age:  20,
	}
	tt.Nil(u1.Add()) // add user1

	u2 := u1 // duplicate user name
	tt.Eq(ErrDuplicateUserName, u2.Add())
	u2.Name = "User2"
	tt.Nil(u2.Add()) // add user2

	f1 := Follow{
		UserId:       u1.Id,
		FollowUserId: 1024000, // user id doesn't exists
	}
	tt.Eq(ErrNoUser, f1.Add())
	f1.FollowUserId = u2.Id
	tt.Nil(f1.Add()) // user1 follow user2

	f2 := Follow{
		UserId:       u2.Id,
		FollowUserId: u1.Id,
	}

	tt.
		Nil(f2.Add()). // user2 follow user1
		Eq(ErrFollowed, f2.Add())

	tt.
		Nil(u1.ById()). // query latest user1 info
		Eq(1, u1.Followers).
		Eq(1, u1.Followings).
		Nil(u2.ById()). // query latest user2 info
		Eq(1, u2.Followers).
		Eq(1, u2.Followings)

	tt.
		Nil(f1.Delete()). // delete follow relationships
		Nil(f2.Delete())

	tt.
		Nil(u1.ById()). // query latest user1 info
		Eq(0, u1.Followings).
		Eq(0, u1.Followings).
		Nil(u2.ById()). // query latest user2 info
		Eq(0, u2.Followings).
		Eq(0, u2.Followings)

	testing2.
		Expect(nil).Arg(u1.Id). // delete user1, user2
		Expect(nil).Arg(u2.Id).
		Run(t, DeleteUserById)
}
示例#26
0
func TestQueryEscape(t *testing.T) {
	tt := testing2.Wrap(t)
	qs := map[string]string{
		"A": "DD",
		"Z": "DD",
	}
	q, _ := QueryEscape(qs, nil)
	s := string(q)
	tt.NE(url.QueryEscape("A=DD&Z=DD") == s, url.QueryEscape("Z=DD&A=DD") == s)
}
示例#27
0
func TestTiming(t *testing.T) {
	tt := testing2.Wrap(t)
	timing := Timing()

	tt.Log(timing())
	tt.Log(timing())
	tt.Log(timing())
	tt.Log(timing())
	tt.Log(timing())
}
示例#28
0
func TestFilterHideHandler(t *testing.T) {
	tt := testing2.Wrap(t)
	rt := NewRouter()
	rt.Handle("/user/12:id", EmptyFilterFunc)
	rt.HandleFunc("/user/:id", GET, EmptyHandlerFunc)
	h, _, _ := rt.MatchHandlerFilters(&url.URL{Path: "/user/1234"})
	tt.True(h == nil)
	h, _, _ = rt.MatchHandlerFilters(&url.URL{Path: "/user/2234"})
	tt.True(h != nil)
}
示例#29
0
func TestExport(t *testing.T) {
	tt := testing2.Wrap(t)
	tt.True(IsExported("Name"))
	tt.True(ToSameExported("Abcd", "abc") == "Abc")
	tt.True(ToSameExported("dbcd", "Abc") == "abc")
	tt.True(ToExported("aaa") == "Aaa")
	tt.True(ToExported("") == "")
	tt.True(ToUnexported("Aaa") == "aaa")
	tt.True(ToUnexported("") == "")
}
示例#30
0
func TestRandString(t *testing.T) {
	tt := testing2.Wrap(t)
	slice := []string{"1", "2", "3", "4"}
	tt.True(sort.SearchStrings(slice, RandIn(slice)) >= 0)
	tt.True(sort.SearchStrings(slice, RandIn(slice)) >= 0)
	tt.True(sort.SearchStrings(slice, RandIn(slice)) >= 0)
	tt.True(sort.SearchStrings(slice, RandIn(slice)) >= 0)
	tt.True(RandIn(nil) == "")
	tt.True(RandIn([]string{}) == "")
}