diff options
| author | Shulhan <ms@kilabit.info> | 2018-09-17 01:21:27 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-09-18 01:50:21 +0700 |
| commit | 44b26edf7f390db383fe025454be0c4e30cfbd9b (patch) | |
| tree | 84d02953bc9095312182534936c1b60667957f07 /lib/tabula/rows_test.go | |
| parent | 4a820ec157501c957d2e30f1670656cceec5c044 (diff) | |
| download | pakakeh.go-44b26edf7f390db383fe025454be0c4e30cfbd9b.tar.xz | |
Merge package "github.com/shuLhan/tabula"
Diffstat (limited to 'lib/tabula/rows_test.go')
| -rw-r--r-- | lib/tabula/rows_test.go | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/lib/tabula/rows_test.go b/lib/tabula/rows_test.go new file mode 100644 index 00000000..174dd10f --- /dev/null +++ b/lib/tabula/rows_test.go @@ -0,0 +1,181 @@ +// Copyright 2017, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be found +// in the LICENSE file. + +package tabula + +import ( + "fmt" + "strings" + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestPushBack(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + exp := strings.Join(rowsExpect, "") + got := fmt.Sprint(rows) + + test.Assert(t, "", exp, got, true) +} + +func TestPopFront(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + l := len(rows) - 1 + for i := range rows { + row := rows.PopFront() + + exp := rowsExpect[i] + got := fmt.Sprint(row) + + test.Assert(t, "", exp, got, true) + + if i < l { + exp = strings.Join(rowsExpect[i+1:], "") + } else { + exp = "" + } + got = fmt.Sprint(rows) + + test.Assert(t, "", exp, got, true) + } + + // empty rows + row := rows.PopFront() + + exp := "<nil>" + got := fmt.Sprint(row) + + test.Assert(t, "", exp, got, true) +} + +func TestPopFrontRow(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + l := len(rows) - 1 + for i := range rows { + newRows := rows.PopFrontAsRows() + + exp := rowsExpect[i] + got := fmt.Sprint(newRows) + + test.Assert(t, "", exp, got, true) + + if i < l { + exp = strings.Join(rowsExpect[i+1:], "") + } else { + exp = "" + } + got = fmt.Sprint(rows) + + test.Assert(t, "", exp, got, true) + } + + // empty rows + row := rows.PopFrontAsRows() + + exp := "" + got := fmt.Sprint(row) + + test.Assert(t, "", exp, got, true) +} + +func TestGroupByValue(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + mapRows := rows.GroupByValue(testClassIdx) + + got := fmt.Sprint(mapRows) + + test.Assert(t, "", groupByExpect, got, true) +} + +func TestRandomPick(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + // random pick with duplicate + for i := 0; i < 5; i++ { + picked, unpicked, pickedIdx, unpickedIdx := rows.RandomPick(6, + true) + + // check if unpicked item exist in picked items. + isin, _ := picked.Contains(unpicked) + + if isin { + fmt.Println("Random pick with duplicate rows") + fmt.Println("==> picked rows :", picked) + fmt.Println("==> picked idx :", pickedIdx) + fmt.Println("==> unpicked rows :", unpicked) + fmt.Println("==> unpicked idx :", unpickedIdx) + t.Fatal("random pick: unpicked is false") + } + } + + // random pick without duplication + for i := 0; i < 5; i++ { + picked, unpicked, pickedIdx, unpickedIdx := rows.RandomPick(3, + false) + + // check if picked rows is duplicate + test.Assert(t, "", picked[0], picked[1], false) + + // check if unpicked item exist in picked items. + isin, _ := picked.Contains(unpicked) + + if isin { + fmt.Println("Random pick with no duplicate rows") + fmt.Println("==> picked rows :", picked) + fmt.Println("==> picked idx :", pickedIdx) + fmt.Println("==> unpicked rows :", unpicked) + fmt.Println("==> unpicked idx :", unpickedIdx) + t.Fatal("random pick: unpicked is false") + } + } +} + +func TestRowsDel(t *testing.T) { + rows, e := initRows() + if e != nil { + t.Fatal(e) + } + + // Test deleting row index out of range. + row := rows.Del(-1) + if row != nil { + t.Fatal("row should be nil!") + } + + row = rows.Del(rows.Len()) + if row != nil { + t.Fatal("row should be nil!") + } + + // Test deleting index that is actually exist. + row = rows.Del(0) + + exp := strings.Join(rowsExpect[1:], "") + got := fmt.Sprint(rows) + + test.Assert(t, "", exp, got, true) + + got = fmt.Sprint(row) + test.Assert(t, "", rowsExpect[0], got, true) +} |
