github.com/vench/word_index@v0.3.1/matrix_index_test.go (about)

     1  package word_index
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestMatrixIndex(t *testing.T) {
     9  	bi := NewMatrixIndex()
    10  	tIndexPlainText(t, bi)
    11  
    12  	bi = NewMatrixIndex()
    13  	tIndexMathText(t, bi)
    14  }
    15  
    16  func TestNewMatrixIndex(t *testing.T) {
    17  
    18  	documents := []string{
    19  		`abc zyz`,
    20  		`test best aaa`,
    21  		`anna vera zoom`,
    22  		`aaa zet zzzz`,
    23  		`This site can’t be reached`,
    24  	}
    25  
    26  	index := NewMatrixIndex()
    27  	err := index.Fit(documents...)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	for _, document := range documents {
    33  		result := index.Query(document)
    34  		if len(result) == 0 {
    35  			t.Fatalf(``)
    36  		}
    37  	}
    38  
    39  	result := index.Query(`aaa`)
    40  	if len(result) != 2 {
    41  		t.Fatalf(`%v`, result)
    42  	}
    43  	if result[0] != 1 {
    44  		t.Fatalf(``)
    45  	}
    46  	if result[1] != 3 {
    47  		t.Fatalf(``)
    48  	}
    49  
    50  	result = index.Query(`vera`)
    51  	if len(result) != 1 {
    52  		t.Fatalf(``)
    53  	}
    54  	if result[0] != 2 {
    55  		t.Fatalf(``)
    56  	}
    57  
    58  	result = index.Query(`reac`)
    59  	if len(result) != 0 {
    60  		t.Fatalf(``)
    61  	}
    62  	result = index.Query(`reac*`)
    63  	if len(result) != 1 {
    64  		t.Fatalf(``)
    65  	}
    66  	if result[0] != 4 {
    67  		t.Fatalf(``)
    68  	}
    69  }
    70  
    71  func TestNewMatrixIndex_MergeOrderedArrayAnd(t *testing.T) {
    72  	res := [][]int{
    73  		{1, 2, 3, 4},
    74  		{1, 3},
    75  		{1, 2, 3, 4, 5, 6},
    76  	}
    77  	ret := MergeOrderedArrayAnd(res)
    78  	if len(ret) != 2 {
    79  		t.Fatalf(`len(ret) != 2, is %d`, len(ret))
    80  	}
    81  	if ret[0] != 1 {
    82  		t.Fatalf(``)
    83  	}
    84  	if ret[1] != 3 {
    85  		t.Fatalf(``)
    86  	}
    87  
    88  	//
    89  	res = [][]int{
    90  		{1, 2, 4, 7, 8, 11},
    91  		{1, 3, 7, 10},
    92  		{1, 2, 3, 4, 5, 6, 7, 10},
    93  	}
    94  	ret = MergeOrderedArrayAnd(res)
    95  	if len(ret) != 2 {
    96  		t.Fatalf(`len(ret) != 2, is %d`, len(ret))
    97  	}
    98  	if ret[0] != 1 {
    99  		t.Fatalf(``)
   100  	}
   101  	if ret[1] != 7 {
   102  		t.Fatalf(``)
   103  	}
   104  
   105  	//
   106  	res = [][]int{
   107  		{0, 2, 4, 7, 8, 11},
   108  		{1, 3, 7, 10},
   109  		{1, 2, 3, 4, 5, 6, 9, 10},
   110  	}
   111  	ret = MergeOrderedArrayAnd(res)
   112  	if len(ret) != 0 {
   113  		t.Fatalf(`len(ret) != 0, is %d`, len(ret))
   114  	}
   115  
   116  	//
   117  	res = [][]int{
   118  		{0, 2, 4},
   119  		{2, 3, 7, 10},
   120  		{1, 2, 3, 4, 5, 6, 9, 10},
   121  	}
   122  	ret = MergeOrderedArrayAnd(res)
   123  	if len(ret) != 1 {
   124  		t.Fatalf(`len(ret) != 0, is %d`, len(ret))
   125  	}
   126  	if ret[0] != 2 {
   127  		t.Fatalf(``)
   128  	}
   129  }
   130  
   131  func TestNewMatrixIndex_MergeOrderArray(t *testing.T) {
   132  	res := [][]int{
   133  		{},
   134  		{1, 2, 4, 5},
   135  		{2, 3, 6, 7, 8},
   136  		{7, 9, 10},
   137  		{1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2},
   138  	}
   139  	ok := []int{
   140  		1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
   141  	}
   142  	r := MergeOrderedArray(res)
   143  	if len(r) != len(ok) {
   144  		fmt.Println(r, ok)
   145  		t.Fatalf(`len(r) != len(ok)`)
   146  	}
   147  	for j := 0; j < len(ok); j++ {
   148  		if ok[j] != r[j] {
   149  			t.Fatalf(``)
   150  		}
   151  	}
   152  
   153  	//
   154  	res = [][]int{
   155  		{},
   156  		{2, 2, 2, 2, 2, 2},
   157  		{1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2},
   158  	}
   159  	ok = []int{
   160  		1, 2,
   161  	}
   162  	r = MergeOrderedArray(res)
   163  	if len(r) != len(ok) {
   164  		t.Fatalf(`len(r) != len(ok)`)
   165  	}
   166  	for j := 0; j < len(ok); j++ {
   167  		if ok[j] != r[j] {
   168  			t.Fatalf(``)
   169  		}
   170  	}
   171  }
   172  
   173  func TestMatrixIndex_QueryAndOr(t *testing.T) {
   174  	documents := []string{
   175  		`abc zyz test site`,
   176  		`test best aaa`,
   177  		`anna vera zoom site`,
   178  		`aaa zet zzzz`,
   179  		`This site test can’t be reached`,
   180  	}
   181  
   182  	index := NewMatrixIndex()
   183  	err := index.Fit(documents...)
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	result := index.QueryAndOr(`test`, true)
   189  	if len(result) != 3 {
   190  		t.Fatalf(``)
   191  	}
   192  	if result[0] != 0 {
   193  		t.Fatalf(``)
   194  	}
   195  	if result[1] != 1 {
   196  		t.Fatalf(``)
   197  	}
   198  	if result[2] != 4 {
   199  		t.Fatalf(``)
   200  	}
   201  	result = index.QueryAndOr(`site`, true)
   202  	if len(result) != 3 {
   203  		t.Fatalf(``)
   204  	}
   205  	if result[0] != 0 {
   206  		t.Fatalf(``)
   207  	}
   208  	if result[1] != 2 {
   209  		t.Fatalf(``)
   210  	}
   211  	if result[2] != 4 {
   212  		t.Fatalf(``)
   213  	}
   214  	result = index.QueryAndOr(`test site`, true)
   215  	if len(result) != 2 {
   216  		t.Fatalf(``)
   217  	}
   218  	if result[0] != 0 {
   219  		t.Fatalf(``)
   220  	}
   221  	if result[1] != 4 {
   222  		t.Fatalf(``)
   223  	}
   224  	result = index.QueryAndOr(`test site`, false)
   225  	if len(result) != 4 {
   226  		t.Fatalf(``)
   227  	}
   228  	if result[0] != 0 {
   229  		t.Fatalf(``)
   230  	}
   231  	if result[1] != 1 {
   232  		t.Fatalf(``)
   233  	}
   234  	if result[2] != 2 {
   235  		t.Fatalf(``)
   236  	}
   237  	if result[3] != 4 {
   238  		t.Fatalf(``)
   239  	}
   240  }