Пример #1
0
func TestPath(t *testing.T) {
	p := Path("/")
	assert.StringEqual(t, "p.Join(abc)", p.Join("abc"), filepath.Join(string(p), "abc"))

	p = "abc.efg"
	assert.StringEqual(t, "p.Exe()", p.Ext(), filepath.Ext(string(p)))
}
Пример #2
0
func TestDocDB(t *testing.T) {
	var db DocDB = PackedDocDB{NewMemDB("", "")}

	info := DocInfo{
		Name: "github.com/daviddengcn/gcse",
	}
	db.Put("hello", info)
	var info2 DocInfo
	if ok := db.Get("hello", &info2); !ok {
		t.Error("db.Get failed!")
		return
	}
	assert.StringEqual(t, "hello", info2, info)

	if err := db.Iterate(func(key string, val interface{}) error {
		info3, ok := val.(DocInfo)
		if !ok {
			return errors.New("errNotDocInfo")
		}

		assert.StringEqual(t, key, info3, info)
		return nil
	}); err != nil {
		t.Errorf("db.Iterate failed: %v", err)
	}
}
Пример #3
0
func TestEditDistanceFFull(t *testing.T) {
	test := func(a, b string, d int, matA []int, matB []int) {
		ra, rb := []rune(a), []rune(b)
		actD, actMatA, actMatB := EditDistanceFFull(len(ra), len(rb),
			func(iA, iB int) int {
				if ra[iA] == rb[iB] {
					return 0
				}

				return 100
			},
			func(iA int) int {
				return 100 + iA
			},
			func(iB int) int {
				return 110 + iB
			})
		assert.Equal(t, fmt.Sprintf("Edit-distance between %s and %s", a, b), actD, d)
		assert.StringEqual(t, fmt.Sprintf("matA for matchting between %s and %s", a, b), actMatA, matA)
		assert.StringEqual(t, fmt.Sprintf("matB for matchting between %s and %s", a, b), actMatB, matB)
	}

	test("abcd", "bcde", 213, []int{-1, 0, 1, 2}, []int{1, 2, 3, -1})
	test("abcde", "", 510, []int{-1, -1, -1, -1, -1}, []int{})
	test("", "abcde", 560, []int{}, []int{-1, -1, -1, -1, -1})
	test("", "", 0, []int{}, []int{})
	test("abcde", "abcde", 0, []int{0, 1, 2, 3, 4}, []int{0, 1, 2, 3, 4})
	test("abcde", "dabce", 213, []int{1, 2, 3, -1, 4}, []int{-1, 0, 1, 2, 4})
	test("abcde", "abfde", 100, []int{0, 1, 2, 3, 4}, []int{0, 1, 2, 3, 4})
}
Пример #4
0
func TestMatchTokens1(t *testing.T) {
	delT, insT := LineToTokens("{abc{def}ghi}"), LineToTokens("{def}")
	assert.StringEqual(t, "delT", delT, "[{ abc { def } ghi }]")
	assert.StringEqual(t, "insT", insT, "[{ def }]")

	matA, matB := MatchTokens(delT, insT)
	assert.StringEqual(t, "matA", matA, "[-1 -1 0 1 2 -1 -1]")
	assert.StringEqual(t, "matB", matB, "[2 3 4]")
}
Пример #5
0
func TestStrDiff(t *testing.T) {
	defer __(o_(t))

	s1 := StringSlice{"a", "b", "d", "f"}
	s2 := StringSlice{"b", "c", "d", "g"}

	d1, d2 := StrValueCompare.DiffSlicePair(s1, s2)
	assert.StringEqual(t, "d1", d1, "[a f]")
	assert.StringEqual(t, "d2", d2, "[c g]")
}
Пример #6
0
func TestNodeToLines_Literal(t *testing.T) {
	info, err := parse("", `
package main

func main() {
	a := Da {
		A: 10,
		B: 20,
	}
}

	`)
	if !assert.NoError(t, err) {
		return
	}

	lines := info.funcs.sourceLines("")
	assert.StringEqual(t, "lines", lines, strings.Split(
		`func main() {
    a := Da{
        A: 10,
        B: 20,
    }
}`, "\n"))
}
Пример #7
0
func TestStrSet_nil(t *testing.T) {
	var s, ss StrSet
	assert.Equal(t, "nil.In(david)", s.In("david"), false)
	assert.Equal(t, "nil.Equals(nil)", s.Equals(ss), true)
	assert.StringEqual(t, "nil.Elements()", s.Elements(), StringSlice{})
	s.Delete("david")
}
Пример #8
0
func TestSplitSentences(t *testing.T) {
	TEXT := `
Package gcse is the core supporting library for go-code-search-engine (GCSE).
Its exported types and functions are mainly for sub packages. If you want
some of the function, copy the code away.

Sub-projects

 crawler  crawling packages

indexer  creating index data for web-server 

 --== Godit - a very religious text editor ==--

server   providing web services, including home/top/search services.
`
	SENTS := []string{
		`Package gcse is the core supporting library for go-code-search-engine (GCSE).`,
		`Its exported types and functions are mainly for sub packages.`,
		`If you want some of the function, copy the code away.`,
		`Sub-projects`,
		`crawler crawling packages`,
		`indexer creating index data for web-server`,
		`Godit - a very religious text editor`,
		`server providing web services, including home/top/search services.`,
	}
	sents := SplitSentences(TEXT)
	assert.StringEqual(t, "Sentences", sents, SENTS)
}
Пример #9
0
func TestSliceRemove(t *testing.T) {
	defer __(o_(t))

	var s Slice
	s.Add(1, 2, 3, 4, 5, 6, 7)
	assert.Equal(t, "len(s)", len(s), 7)
	assert.StringEqual(t, "s", s, "[1 2 3 4 5 6 7]")

	s.RemoveRange(2, 5)
	assert.Equal(t, "len(s)", len(s), 4)
	assert.StringEqual(t, "s", s, "[1 2 6 7]")

	s.Remove(2)
	assert.Equal(t, "len(s)", len(s), 3)
	assert.StringEqual(t, "s", s, "[1 2 7]")
}
Пример #10
0
func TestDocDB_Export(t *testing.T) {
	var db DocDB = PackedDocDB{NewMemDB("", "")}

	info := DocInfo{
		Name: "github.com/daviddengcn/gcse",
	}

	db.Put("go", info)

	if err := db.Export(villa.Path("."), "testexport_db"); err != nil {
		t.Errorf("db.Export failed: %v", err)
		return
	}

	var newDB DocDB = PackedDocDB{NewMemDB(villa.Path("."), "testexport_db")}
	count := 0
	if err := newDB.Iterate(func(key string, val interface{}) error {
		info, ok := val.(DocInfo)
		if !ok {
			return errors.New("Not a DocInfo object")
		}
		assert.StringEqual(t, "info.Name", info.Name,
			"github.com/daviddengcn/gcse")
		count++
		return nil
	}); err != nil {
		t.Errorf("newDB.Iterate failed: %v", err)
	}

	assert.Equal(t, "count", count, 1)
}
Пример #11
0
func TestDiffLines_2(t *testing.T) {
	var buf bytesp.Slice
	gOut = &buf

	src := strings.Split(`abc
{
  hello
}
defg`, "\n")
	dst := strings.Split(`abc
{
  gogogo
}
{
  hello
}
defg`, "\n")
	diffLines(src, dst, "%s")

	t.Logf("Diff: %s", string(buf))

	assert.StringEqual(t, "diff", strings.Split(string(buf), "\n"),
		strings.Split(`    abc
+++ {
+++   gogogo
+++ }
    {
        ... (2 lines)
    defg
`, "\n"))
}
Пример #12
0
func TestIntMatrix(t *testing.T) {
	defer __(o_(t))

	mat := NewIntMatrix(5, 4)
	assert.Equal(t, "mat.Rows()", mat.Rows(), 5)
	assert.Equal(t, "mat.Cols()", mat.Cols(), 4)
	assert.StringEqual(t, "mat", mat, "[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]")

	mat.Fill(10)
	assert.StringEqual(t, "mat", mat, "[[10 10 10 10] [10 10 10 10] [10 10 10 10] [10 10 10 10] [10 10 10 10]]")

	mat[1][1] = 0
	mat[3][2] = 12345
	mat[2][0] = -998
	t.Logf("%s", mat.PrettyString())
}
Пример #13
0
func TestEditDistanceFull(t *testing.T) {
	test := func(a, b string, d int, matA, matB []int) {
		actD, actMatA, actMatB := EditDistanceFull(&stringInterface{[]rune(a), []rune(b)})
		assert.Equal(t, fmt.Sprintf("Edit-distance between %s and %s", a, b), actD, d)
		assert.StringEqual(t, fmt.Sprintf("matA for matchting between %s and %s", a, b), actMatA, matA)
		assert.StringEqual(t, fmt.Sprintf("matB for matchting between %s and %s", a, b), actMatB, matB)
	}

	test("abcd", "bcde", 213, []int{-1, 0, 1, 2}, []int{1, 2, 3, -1})
	test("abcde", "", 510, []int{-1, -1, -1, -1, -1}, []int{})
	test("", "abcde", 560, []int{}, []int{-1, -1, -1, -1, -1})
	test("", "", 0, []int{}, []int{})
	test("abcde", "abcde", 0, []int{0, 1, 2, 3, 4}, []int{0, 1, 2, 3, 4})
	test("abcde", "dabce", 213, []int{1, 2, 3, -1, 4}, []int{-1, 0, 1, 2, 4})
	test("abcde", "abfde", 100, []int{0, 1, 2, 3, 4}, []int{0, 1, 2, 3, 4})
}
Пример #14
0
func TestStringSliceRemove(t *testing.T) {
	defer __(o_(t))

	var s StringSlice
	s.Add("A", "B", "C", "D", "E", "F", "G")
	assert.Equal(t, "len(s)", len(s), 7)
	assert.StringEqual(t, "s", s, "[A B C D E F G]")

	s.RemoveRange(2, 5)
	assert.Equal(t, "len(s)", len(s), 4)
	assert.StringEqual(t, "s", s, "[A B F G]")

	s.Remove(2)
	assert.Equal(t, "len(s)", len(s), 3)
	assert.StringEqual(t, "s", s, "[A B G]")
}
Пример #15
0
func TestIntMatrix_Clone(t *testing.T) {
	// Clone for null matrix, assure no panic
	mat := IntMatrix(nil)
	mat.Clone()
	mat.Fill(0)
	assert.Equal(t, "mat.Rows()", mat.Rows(), 0)
	assert.Equal(t, "mat.Cols()", mat.Cols(), 0)

	// Clone for non-null matrix
	mat = NewIntMatrix(2, 2)
	mat.Fill(1)
	matB := mat.Clone()
	// Change contents of mat, matB should not be changed.
	mat.Fill(2)
	assert.StringEqual(t, "mat", mat, "[[2 2] [2 2]]")
	assert.StringEqual(t, "matB", matB, "[[1 1] [1 1]]")
}
Пример #16
0
func TestSortF(t *testing.T) {
	ints := []int{3, 4, 1, 7, 0}
	SortF(len(ints), func(i, j int) bool {
		return ints[i] < ints[j]
	}, func(i, j int) {
		ints[i], ints[j] = ints[j], ints[i]
	})
	assert.StringEqual(t, "ints", ints, []int{0, 1, 3, 4, 7})

	ints = []int{3, 4, 1, 7, 0}
	SortF(len(ints), func(i, j int) bool {
		return ints[i] > ints[j]
	}, func(i, j int) {
		ints[i], ints[j] = ints[j], ints[i]
	})
	assert.StringEqual(t, "ints", ints, []int{7, 4, 3, 1, 0})
}
Пример #17
0
func TestDocInfo(t *testing.T) {
	src := DocInfo{
		Name:        "gcse",
		Package:     "github.com/daviddengcn/gcse",
		Author:      "github.com/daviddengcn",
		LastUpdated: time.Now(),
		StarCount:   10,
		Synopsis:    "Go Package Search Engine",
		Description: "More details about GCSE",
		ProjectURL:  "http://github.com/daviddengcn/gcse",
		ReadmeFn:    "readme.txt",
		ReadmeData:  "Just read me",
		Imports: []string{
			"github.com/daviddengcn/go-villa",
			"github.com/daviddengcn/sophie",
		},
		TestImports: []string{
			"github.com/daviddengcn/go-check",
		},
		Exported: []string{
			"DocInfo", "CheckRuneType",
		},
	}
	var buf bytesp.Slice
	assert.NoError(t, src.WriteTo(&buf))

	var dst DocInfo
	assert.NoError(t, dst.ReadFrom(&buf, -1))

	assert.StringEqual(t, "dst", dst, src)

	// checking the bug introduced by reusing slice
	dst2 := dst
	assert.StringEqual(t, "dst2.Imports[0]", dst2.Imports[0],
		"github.com/daviddengcn/go-villa")

	src.Imports[0] = "github.com/daviddengcn/go-assert"
	buf = nil
	assert.NoError(t, src.WriteTo(&buf))
	assert.NoError(t, dst.ReadFrom(&buf, -1))
	assert.StringEqual(t, "dst", dst, src)

	assert.StringEqual(t, "dst2.Imports[0]", dst2.Imports[0],
		"github.com/daviddengcn/go-villa")
}
Пример #18
0
func TestComplexSliceRemove(t *testing.T) {
	defer __(o_(t))

	var s ComplexSlice
	s.Add(-1, -2, -3, -4, -5, -6, -7)
	assert.Equal(t, "len", len(s), 7)
	assert.StringEqual(t, "s", s, "[(-1+0i) (-2+0i) (-3+0i) (-4+0i) (-5+0i) (-6+0i) (-7+0i)]")

	s.Fill(2, 5, -9-8i)
	assert.StringEqual(t, "s", s, "[(-1+0i) (-2+0i) (-9-8i) (-9-8i) (-9-8i) (-6+0i) (-7+0i)]")

	s.RemoveRange(2, 5)
	assert.Equal(t, "len", len(s), 4)
	assert.StringEqual(t, "s", s, "[(-1+0i) (-2+0i) (-6+0i) (-7+0i)]")

	s.Remove(2)
	assert.Equal(t, "len", len(s), 3)
	assert.StringEqual(t, "s", s, "[(-1+0i) (-2+0i) (-7+0i)]")
}
Пример #19
0
func TestChooseImportantSentenses_GoBot(t *testing.T) {
	TEXT := `
GoBot is an IRC Bot programmed in Golang![Build Status](https://secure.travis-ci.org/prometheus/client_golang.png?branch=master). It is designed to be lightweight and fast.
`
	IMPORTANTS := []string{
		`GoBot is an IRC Bot programmed in Golang.`,
	}
	importants := ChooseImportantSentenses(TEXT, "main", "github.com/wei2912/GoBot")
	assert.StringEqual(t, "importants", importants, IMPORTANTS)
}
Пример #20
0
func TestChooseImportantSentenses_PackageEscape(t *testing.T) {
	TEXT := `
GoBot is an IRC Bot programmed.
`
	IMPORTANTS := []string{
		`GoBot is an IRC Bot programmed.`,
	}
	importants := ChooseImportantSentenses(TEXT, "main", "github.com/+wei2912/GoBot")
	assert.StringEqual(t, "importants", importants, IMPORTANTS)
}
Пример #21
0
func TestIntMatrix(t *testing.T) {
	defer __(o_(t))

	mat := NewIntMatrix(5, 4)
	assert.Equal(t, "mat.Rows()", mat.Rows(), 5)
	assert.Equal(t, "mat.Cols()", mat.Cols(), 4)
	assert.StringEqual(t, "mat", mat, "[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]")

	mat.Fill(10)
	assert.StringEqual(t, "mat", mat, "[[10 10 10 10] [10 10 10 10] [10 10 10 10] [10 10 10 10] [10 10 10 10]]")

	mat[1][1] = 0
	mat[3][2] = 12345
	mat[2][0] = -998
	t.Logf("%s", mat.PrettyString())

	// Clone for null matrix, assure no panic
	mat = IntMatrix(nil)
	mat.Clone()
	mat.Fill(0)
	assert.Equal(t, "mat.Rows()", mat.Rows(), 0)
	assert.Equal(t, "mat.Cols()", mat.Cols(), 0)
}
Пример #22
0
func TestCrawlingEntry(t *testing.T) {
	src := CrawlingEntry{
		ScheduleTime: time.Now(),
		Version:      19,
		Etag:         "Hello",
	}

	var buf bytesp.Slice
	assert.NoError(t, src.WriteTo(&buf))

	var dst CrawlingEntry
	assert.NoError(t, dst.ReadFrom(&buf, -1))

	assert.StringEqual(t, "dst", dst, src)
}
Пример #23
0
func TestChooseImportantSentenses(t *testing.T) {
	TEXT := `
gcse implements something. If you want some of the function, copy the code away.

Package gcse provides something

daviddengcn/core is a something

github/daviddengcn/core is more than a something
-------------------------------------------------
This is a something

gcse是一个something

gcse 是一个something

 is a framework to compare the performance of go 1.0 (go 1.0.3) and go 1.1 (go +tip).

这是一个something

非这是一个something2

the core package provides something

Go language implementation of selected algorithms from the

A simple pluggable lexer package.
`
	IMPORTANTS := []string{
		`gcse implements something.`,
		`Package gcse provides something`,
		`daviddengcn/core is a something`,
		`github/daviddengcn/core is more than a something`,
		`This is a something`,
		`gcse是一个something`,
		`gcse 是一个something`,
		`is a framework to compare the performance of go 1.0 (go 1.0.3) and go 1.1 (go +tip).`,
		`这是一个something`,
		`the core package provides something`,
		`Go language implementation of selected algorithms from the`,
		`A simple pluggable lexer package.`,
	}
	importants := ChooseImportantSentenses(TEXT, "gcse", "github/daviddengcn/core")
	assert.StringEqual(t, "importants", importants, IMPORTANTS)
}
Пример #24
0
func TestSlice(t *testing.T) {
	defer __(o_(t))

	var s Slice
	for i := 0; i < 1000; i++ {
		s.Add(i)
	}

	assert.Equal(t, "len(s)", len(s), 1000)
	s.Clear()
	assert.Equal(t, "len(s)", len(s), 0)

	s = Slice{}
	s.Add(4, 1)
	s.Insert(1, 2, 3)
	assert.Equal(t, "len(s)", len(s), 4)
	assert.StringEqual(t, "s", s, "[4 2 3 1]")
}
Пример #25
0
func TestStringSlice(t *testing.T) {
	defer __(o_(t))

	var s StringSlice
	for i := 0; i < 1000; i++ {
		s.Add(string('A' + i))
	}

	assert.Equal(t, "len(s)", len(s), 1000)
	s.Clear()
	assert.Equal(t, "len(s)", len(s), 0)

	s = StringSlice{}
	s.Add("E", "B")
	s.Insert(1, "C", "D")
	assert.Equal(t, "len(s)", len(s), 4)
	assert.StringEqual(t, "s", s, "[E C D B]")
}
Пример #26
0
func TestComplexSlice(t *testing.T) {
	defer __(o_(t))

	var s ComplexSlice
	for i := 0; i < 1000; i++ {
		s.Add(complex(float64(i), float64(-i)))
	} // for i

	assert.Equal(t, "len", len(s), 1000)
	//fmt.Println(s)
	s.Clear()
	assert.Equal(t, "len", len(s), 0)

	s = ComplexSlice{}
	s.Add(-4, -1)
	s.Insert(1, -2, -3)
	t.Logf("%v", s)
	assert.Equal(t, "len", len(s), 4)
	assert.StringEqual(t, "s", s, "[(-4+0i) (-2+0i) (-3+0i) (-1+0i)]")
}
Пример #27
0
func TestFloatSlice(t *testing.T) {
	defer __(o_(t))

	var s FloatSlice
	for i := 0; i < 1000; i++ {
		s.Add(float64(i))
	} // for i

	assert.Equal(t, "len", len(s), 1000)
	//fmt.Println(s)
	s.Clear()
	assert.Equal(t, "len", len(s), 0)

	s = FloatSlice{}
	s.Add(1)
	s.Insert(0, 2)
	s.Insert(1, 3)
	t.Logf("%v", s)
	assert.Equal(t, "len", len(s), 3)
	assert.StringEqual(t, "s", s, "[2 3 1]")
}
Пример #28
0
func TestMergeComplex(t *testing.T) {
	defer __(o_(t))

	var a, b ComplexSlice
	for i := 0; i < 100; i++ {
		a.Add(complex(rand.Float64(), rand.Float64()))
	} // for i
	for i := 0; i < 200; i++ {
		b.Add(complex(rand.Float64(), rand.Float64()))
	} // for i

	var cc ComplexSlice
	cc.Add(a...)
	cc.Add(b...)
	cmplexAbsCmpFunc.Sort(cc)

	cmplexAbsCmpFunc.Sort(a)
	cmplexAbsCmpFunc.Sort(b)

	c := cmplexAbsCmpFunc.Merge(a, b)
	assert.Equal(t, "len(c)", len(c), len(cc))
	assert.StringEqual(t, "c", c, cc)
}
Пример #29
0
func TestMergeString(t *testing.T) {
	defer __(o_(t))

	var a, b StringSlice
	for i := 0; i < 100; i++ {
		a.Add(fmt.Sprint(rand.Int()))
	} // for i
	for i := 0; i < 200; i++ {
		b.Add(fmt.Sprint(rand.Int()))
	} // for i

	var cc StringSlice
	cc.InsertSlice(0, a)
	cc.InsertSlice(0, b)
	StrValueCompare.Sort(cc)

	StrValueCompare.Sort(a)
	StrValueCompare.Sort(b)

	c := StrValueCompare.Merge(a, b)
	assert.Equal(t, "len(c)", len(c), len(cc))
	assert.StringEqual(t, "c", c, cc)
}
Пример #30
0
func TestMergeFloat(t *testing.T) {
	defer __(o_(t))

	var a, b FloatSlice
	for i := 0; i < 100; i++ {
		a.Add(rand.Float64())
	} // for i
	for i := 0; i < 200; i++ {
		b.Add(rand.Float64())
	} // for i

	var cc FloatSlice
	cc.Add(a...)
	cc.Add(b...)
	FloatValueCompare.Sort(cc)

	FloatValueCompare.Sort(a)
	FloatValueCompare.Sort(b)

	c := FloatValueCompare.Merge(a, b)
	assert.Equal(t, "len(c)", len(c), len(cc))
	assert.StringEqual(t, "c", c, cc)
}