github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/encoding/docs/index_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package docs 22 23 import ( 24 "bytes" 25 "testing" 26 27 "github.com/m3db/m3/src/m3ninx/postings" 28 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestStoredFieldsIndex(t *testing.T) { 33 type entry struct { 34 id postings.ID 35 offset uint64 36 } 37 38 tests := []struct { 39 name string 40 entries []entry 41 }{ 42 { 43 name: "no offsets", 44 entries: []entry{}, 45 }, 46 { 47 name: "single offset", 48 entries: []entry{ 49 entry{id: 0, offset: 0}, 50 }, 51 }, 52 { 53 name: "single offset with non-zero base", 54 entries: []entry{ 55 entry{id: 65, offset: 12}, 56 }, 57 }, 58 { 59 name: "multiple offsets", 60 entries: []entry{ 61 entry{id: 0, offset: 0}, 62 entry{id: 1, offset: 20}, 63 entry{id: 6, offset: 50}, 64 entry{id: 18, offset: 100}, 65 entry{id: 19, offset: 125}, 66 }, 67 }, 68 { 69 name: "multiple offsets with non-zero base", 70 entries: []entry{ 71 entry{id: 42, offset: 0}, 72 entry{id: 43, offset: 33}, 73 entry{id: 44, offset: 77}, 74 entry{id: 45, offset: 88}, 75 entry{id: 49, offset: 111}, 76 }, 77 }, 78 { 79 name: "non-monotonic offsets", 80 entries: []entry{ 81 entry{id: 13, offset: 124}, 82 entry{id: 14, offset: 28}, 83 entry{id: 15, offset: 95}, 84 entry{id: 17, offset: 56}, 85 entry{id: 20, offset: 77}, 86 }, 87 }, 88 } 89 90 w := NewIndexWriter(nil) 91 for _, test := range tests { 92 t.Run(test.name, func(t *testing.T) { 93 buf := new(bytes.Buffer) 94 w.Reset(buf) 95 96 for i := range test.entries { 97 id, offset := test.entries[i].id, test.entries[i].offset 98 err := w.Write(id, offset) 99 require.NoError(t, err) 100 } 101 102 r, err := NewIndexReader(buf.Bytes()) 103 require.NoError(t, err) 104 for i := range test.entries { 105 if i == 0 { 106 require.Equal(t, test.entries[i].id, r.Base()) 107 } 108 id, offset := test.entries[i].id, test.entries[i].offset 109 actual, err := r.Read(id) 110 require.NoError(t, err) 111 require.Equal(t, offset, actual) 112 } 113 114 var expectedLen int 115 if len(test.entries) == 0 { 116 expectedLen = 0 117 } else { 118 start := test.entries[0].id 119 end := test.entries[len(test.entries)-1].id 120 expectedLen = int(end-start) + 1 121 } 122 require.Equal(t, expectedLen, r.Len()) 123 }) 124 } 125 }