postings_test.go 15.5 KB
Newer Older
F
Fabian Reinartz 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

14
package index
F
Fabian Reinartz 已提交
15 16

import (
17
	"encoding/binary"
18
	"fmt"
19
	"math/rand"
20
	"sort"
F
Fabian Reinartz 已提交
21
	"testing"
22

23
	"github.com/prometheus/prometheus/tsdb/testutil"
F
Fabian Reinartz 已提交
24 25
)

26
func TestMemPostings_addFor(t *testing.T) {
27
	p := NewMemPostings()
B
Brian Brazil 已提交
28 29
	p.m[allPostingsKey.Name] = map[string][]uint64{}
	p.m[allPostingsKey.Name][allPostingsKey.Value] = []uint64{1, 2, 3, 4, 6, 7, 8}
30 31 32

	p.addFor(5, allPostingsKey)

B
Brian Brazil 已提交
33
	testutil.Equals(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8}, p.m[allPostingsKey.Name][allPostingsKey.Value])
34 35
}

36
func TestMemPostings_ensureOrder(t *testing.T) {
37
	p := NewUnorderedMemPostings()
B
Brian Brazil 已提交
38
	p.m["a"] = map[string][]uint64{}
39 40 41 42 43 44 45 46

	for i := 0; i < 100; i++ {
		l := make([]uint64, 100)
		for j := range l {
			l[j] = rand.Uint64()
		}
		v := fmt.Sprintf("%d", i)

B
Brian Brazil 已提交
47
		p.m["a"][v] = l
48 49
	}

50
	p.EnsureOrder()
51

B
Brian Brazil 已提交
52 53 54 55 56 57 58 59
	for _, e := range p.m {
		for _, l := range e {
			ok := sort.SliceIsSorted(l, func(i, j int) bool {
				return l[i] < l[j]
			})
			if !ok {
				t.Fatalf("postings list %v is not sorted", l)
			}
60 61 62 63
		}
	}
}

F
Fabian Reinartz 已提交
64
func TestIntersect(t *testing.T) {
65 66 67
	a := newListPostings(1, 2, 3)
	b := newListPostings(2, 3, 4)

F
Fabian Reinartz 已提交
68
	var cases = []struct {
69 70 71
		in []Postings

		res Postings
F
Fabian Reinartz 已提交
72 73
	}{
		{
74 75
			in:  []Postings{},
			res: EmptyPostings(),
F
Fabian Reinartz 已提交
76 77
		},
		{
78 79
			in:  []Postings{a, b, EmptyPostings()},
			res: EmptyPostings(),
F
Fabian Reinartz 已提交
80 81
		},
		{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			in:  []Postings{b, a, EmptyPostings()},
			res: EmptyPostings(),
		},
		{
			in:  []Postings{EmptyPostings(), b, a},
			res: EmptyPostings(),
		},
		{
			in:  []Postings{EmptyPostings(), a, b},
			res: EmptyPostings(),
		},
		{
			in:  []Postings{a, EmptyPostings(), b},
			res: EmptyPostings(),
		},
		{
			in:  []Postings{b, EmptyPostings(), a},
			res: EmptyPostings(),
		},
		{
			in:  []Postings{b, EmptyPostings(), a, a, b, a, a, a},
			res: EmptyPostings(),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 5),
				newListPostings(6, 7, 8, 9, 10),
			},
			res: newListPostings(),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 5),
				newListPostings(4, 5, 6, 7, 8),
			},
			res: newListPostings(4, 5),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 9, 10),
				newListPostings(1, 4, 5, 6, 7, 8, 10, 11),
			},
			res: newListPostings(1, 4, 10),
		},
		{
			in: []Postings{
				newListPostings(1),
				newListPostings(0, 1),
			},
			res: newListPostings(1),
		},
		{
			in: []Postings{
				newListPostings(1),
			},
			res: newListPostings(1),
		},
		{
			in: []Postings{
				newListPostings(1),
				newListPostings(),
			},
			res: newListPostings(),
		},
		{
			in: []Postings{
				newListPostings(),
				newListPostings(),
			},
			res: newListPostings(),
F
Fabian Reinartz 已提交
152 153 154
		},
	}

155
	for _, c := range cases {
156 157 158 159
		t.Run("", func(t *testing.T) {
			if c.res == nil {
				t.Fatal("intersect result expectancy cannot be nil")
			}
F
Fabian Reinartz 已提交
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
			expected, err := ExpandPostings(c.res)
			testutil.Ok(t, err)

			i := Intersect(c.in...)

			if c.res == EmptyPostings() {
				testutil.Equals(t, EmptyPostings(), i)
				return
			}

			if i == EmptyPostings() {
				t.Fatal("intersect unexpected result: EmptyPostings sentinel")
			}

			res, err := ExpandPostings(i)
			testutil.Ok(t, err)
			testutil.Equals(t, expected, res)
		})
F
Fabian Reinartz 已提交
179 180 181 182 183
	}
}

func TestMultiIntersect(t *testing.T) {
	var cases = []struct {
184 185
		p   [][]uint64
		res []uint64
F
Fabian Reinartz 已提交
186 187
	}{
		{
188
			p: [][]uint64{
189 190 191 192
				{1, 2, 3, 4, 5, 6, 1000, 1001},
				{2, 4, 5, 6, 7, 8, 999, 1001},
				{1, 2, 5, 6, 7, 8, 1001, 1200},
			},
193
			res: []uint64{2, 5, 6, 1001},
F
Fabian Reinartz 已提交
194
		},
195 196 197 198 199
		// One of the reproduceable cases for:
		// https://github.com/prometheus/prometheus/issues/2616
		// The initialisation of intersectPostings was moving the iterator forward
		// prematurely making us miss some postings.
		{
200
			p: [][]uint64{
201 202 203 204 205
				{1, 2},
				{1, 2},
				{1, 2},
				{2},
			},
206
			res: []uint64{2},
207
		},
F
Fabian Reinartz 已提交
208 209 210
	}

	for _, c := range cases {
211 212
		ps := make([]Postings, 0, len(c.p))
		for _, postings := range c.p {
213
			ps = append(ps, newListPostings(postings...))
214
		}
F
Fabian Reinartz 已提交
215

216
		res, err := ExpandPostings(Intersect(ps...))
217

C
Callum Styan 已提交
218 219
		testutil.Ok(t, err)
		testutil.Equals(t, c.res, res)
F
Fabian Reinartz 已提交
220 221 222 223
	}
}

func BenchmarkIntersect(t *testing.B) {
224 225
	t.Run("LongPostings1", func(bench *testing.B) {
		var a, b, c, d []uint64
F
Fabian Reinartz 已提交
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		for i := 0; i < 10000000; i += 2 {
			a = append(a, uint64(i))
		}
		for i := 5000000; i < 5000100; i += 4 {
			b = append(b, uint64(i))
		}
		for i := 5090000; i < 5090600; i += 4 {
			b = append(b, uint64(i))
		}
		for i := 4990000; i < 5100000; i++ {
			c = append(c, uint64(i))
		}
		for i := 4000000; i < 6000000; i++ {
			d = append(d, uint64(i))
		}
F
Fabian Reinartz 已提交
242

243 244 245 246
		i1 := newListPostings(a...)
		i2 := newListPostings(b...)
		i3 := newListPostings(c...)
		i4 := newListPostings(d...)
F
Fabian Reinartz 已提交
247

248 249 250 251 252 253 254 255 256 257 258
		bench.ResetTimer()
		bench.ReportAllocs()
		for i := 0; i < bench.N; i++ {
			if _, err := ExpandPostings(Intersect(i1, i2, i3, i4)); err != nil {
				bench.Fatal(err)
			}
		}
	})

	t.Run("LongPostings2", func(bench *testing.B) {
		var a, b, c, d []uint64
F
Fabian Reinartz 已提交
259

260 261
		for i := 0; i < 12500000; i++ {
			a = append(a, uint64(i))
F
Fabian Reinartz 已提交
262
		}
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		for i := 7500000; i < 12500000; i++ {
			b = append(b, uint64(i))
		}
		for i := 9000000; i < 20000000; i++ {
			c = append(c, uint64(i))
		}
		for i := 10000000; i < 12000000; i++ {
			d = append(d, uint64(i))
		}

		i1 := newListPostings(a...)
		i2 := newListPostings(b...)
		i3 := newListPostings(c...)
		i4 := newListPostings(d...)

		bench.ResetTimer()
		bench.ReportAllocs()
		for i := 0; i < bench.N; i++ {
			if _, err := ExpandPostings(Intersect(i1, i2, i3, i4)); err != nil {
				bench.Fatal(err)
			}
		}
	})

	// Many matchers(k >> n).
	t.Run("ManyPostings", func(bench *testing.B) {
		var its []Postings

		// 100000 matchers(k=100000).
		for i := 0; i < 100000; i++ {
			var temp []uint64
			for j := 1; j < 100; j++ {
				temp = append(temp, uint64(j))
			}
			its = append(its, newListPostings(temp...))
		}

		bench.ResetTimer()
		bench.ReportAllocs()
		for i := 0; i < bench.N; i++ {
			if _, err := ExpandPostings(Intersect(its...)); err != nil {
				bench.Fatal(err)
			}
		}
	})
F
Fabian Reinartz 已提交
308
}
F
Fabian Reinartz 已提交
309 310

func TestMultiMerge(t *testing.T) {
311 312 313
	i1 := newListPostings(1, 2, 3, 4, 5, 6, 1000, 1001)
	i2 := newListPostings(2, 4, 5, 6, 7, 8, 999, 1001)
	i3 := newListPostings(1, 2, 5, 6, 7, 8, 1001, 1200)
F
Fabian Reinartz 已提交
314

315 316 317
	res, err := ExpandPostings(Merge(i1, i2, i3))
	testutil.Ok(t, err)
	testutil.Equals(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 999, 1000, 1001, 1200}, res)
F
Fabian Reinartz 已提交
318 319
}

320
func TestMergedPostings(t *testing.T) {
F
Fabian Reinartz 已提交
321
	var cases = []struct {
322 323 324
		in []Postings

		res Postings
F
Fabian Reinartz 已提交
325 326
	}{
		{
327 328
			in:  []Postings{},
			res: EmptyPostings(),
F
Fabian Reinartz 已提交
329 330
		},
		{
331 332 333 334 335
			in: []Postings{
				newListPostings(),
				newListPostings(),
			},
			res: EmptyPostings(),
F
Fabian Reinartz 已提交
336 337
		},
		{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			in: []Postings{
				newListPostings(),
			},
			res: newListPostings(),
		},
		{
			in: []Postings{
				EmptyPostings(),
				EmptyPostings(),
				EmptyPostings(),
				EmptyPostings(),
			},
			res: EmptyPostings(),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 5),
				newListPostings(6, 7, 8, 9, 10),
			},
			res: newListPostings(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 5),
				newListPostings(4, 5, 6, 7, 8),
			},
			res: newListPostings(1, 2, 3, 4, 5, 6, 7, 8),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 9, 10),
				newListPostings(1, 4, 5, 6, 7, 8, 10, 11),
			},
			res: newListPostings(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
		},
		{
			in: []Postings{
				newListPostings(1, 2, 3, 4, 9, 10),
				EmptyPostings(),
				newListPostings(1, 4, 5, 6, 7, 8, 10, 11),
			},
			res: newListPostings(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
		},
		{
			in: []Postings{
				newListPostings(1, 2),
				newListPostings(),
			},
			res: newListPostings(1, 2),
		},
		{
			in: []Postings{
				newListPostings(1, 2),
				EmptyPostings(),
			},
			res: newListPostings(1, 2),
F
Fabian Reinartz 已提交
394 395 396 397
		},
	}

	for _, c := range cases {
398 399 400 401
		t.Run("", func(t *testing.T) {
			if c.res == nil {
				t.Fatal("merge result expectancy cannot be nil")
			}
F
Fabian Reinartz 已提交
402

403 404 405 406
			expected, err := ExpandPostings(c.res)
			testutil.Ok(t, err)

			m := Merge(c.in...)
407

408 409 410 411 412 413 414 415 416 417 418 419 420 421
			if c.res == EmptyPostings() {
				testutil.Equals(t, EmptyPostings(), m)
				return
			}

			if m == EmptyPostings() {
				t.Fatal("merge unexpected result: EmptyPostings sentinel")
			}

			res, err := ExpandPostings(m)
			testutil.Ok(t, err)
			testutil.Equals(t, expected, res)
		})
	}
422
}
423

424 425
func TestMergedPostingsSeek(t *testing.T) {
	var cases = []struct {
426
		a, b []uint64
427

428
		seek    uint64
429
		success bool
430
		res     []uint64
431 432
	}{
		{
433 434
			a: []uint64{2, 3, 4, 5},
			b: []uint64{6, 7, 8, 9, 10},
435

436
			seek:    1,
437
			success: true,
438
			res:     []uint64{2, 3, 4, 5, 6, 7, 8, 9, 10},
439 440
		},
		{
441 442
			a: []uint64{1, 2, 3, 4, 5},
			b: []uint64{6, 7, 8, 9, 10},
443

444 445
			seek:    2,
			success: true,
446
			res:     []uint64{2, 3, 4, 5, 6, 7, 8, 9, 10},
447 448
		},
		{
449 450
			a: []uint64{1, 2, 3, 4, 5},
			b: []uint64{4, 5, 6, 7, 8},
451

452 453 454 455 456
			seek:    9,
			success: false,
			res:     nil,
		},
		{
457 458
			a: []uint64{1, 2, 3, 4, 9, 10},
			b: []uint64{1, 4, 5, 6, 7, 8, 10, 11},
459

460 461
			seek:    10,
			success: true,
462
			res:     []uint64{10, 11},
463 464 465 466
		},
	}

	for _, c := range cases {
467 468
		a := newListPostings(c.a...)
		b := newListPostings(c.b...)
469

470
		p := Merge(a, b)
471

C
Callum Styan 已提交
472
		testutil.Equals(t, c.success, p.Seek(c.seek))
473 474 475 476

		// After Seek(), At() should be called.
		if c.success {
			start := p.At()
477
			lst, err := ExpandPostings(p)
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
			testutil.Ok(t, err)

			lst = append([]uint64{start}, lst...)
			testutil.Equals(t, c.res, lst)
		}
	}
}

func TestRemovedPostings(t *testing.T) {
	var cases = []struct {
		a, b []uint64
		res  []uint64
	}{
		{
			a:   nil,
			b:   nil,
			res: []uint64(nil),
		},
		{
			a:   []uint64{1, 2, 3, 4},
			b:   nil,
			res: []uint64{1, 2, 3, 4},
		},
		{
			a:   nil,
			b:   []uint64{1, 2, 3, 4},
			res: []uint64(nil),
		},
		{
			a:   []uint64{1, 2, 3, 4, 5},
			b:   []uint64{6, 7, 8, 9, 10},
			res: []uint64{1, 2, 3, 4, 5},
		},
		{
			a:   []uint64{1, 2, 3, 4, 5},
			b:   []uint64{4, 5, 6, 7, 8},
			res: []uint64{1, 2, 3},
		},
		{
			a:   []uint64{1, 2, 3, 4, 9, 10},
			b:   []uint64{1, 4, 5, 6, 7, 8, 10, 11},
			res: []uint64{2, 3, 9},
		},
		{
			a:   []uint64{1, 2, 3, 4, 9, 10},
			b:   []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
			res: []uint64(nil),
		},
	}

	for _, c := range cases {
529 530
		a := newListPostings(c.a...)
		b := newListPostings(c.b...)
531

532
		res, err := ExpandPostings(newRemovedPostings(a, b))
533 534 535 536 537 538
		testutil.Ok(t, err)
		testutil.Equals(t, c.res, res)
	}

}

539 540 541 542 543 544 545 546 547 548
func TestRemovedNextStackoverflow(t *testing.T) {
	var full []uint64
	var remove []uint64

	var i uint64
	for i = 0; i < 1e7; i++ {
		full = append(full, i)
		remove = append(remove, i)
	}

549 550
	flp := newListPostings(full...)
	rlp := newListPostings(remove...)
551 552 553 554 555
	rp := newRemovedPostings(flp, rlp)
	gotElem := false
	for rp.Next() {
		gotElem = true
	}
A
Alexey Palazhchenko 已提交
556

557 558 559 560
	testutil.Ok(t, rp.Err())
	testutil.Assert(t, !gotElem, "")
}

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
func TestRemovedPostingsSeek(t *testing.T) {
	var cases = []struct {
		a, b []uint64

		seek    uint64
		success bool
		res     []uint64
	}{
		{
			a: []uint64{2, 3, 4, 5},
			b: []uint64{6, 7, 8, 9, 10},

			seek:    1,
			success: true,
			res:     []uint64{2, 3, 4, 5},
		},
		{
			a: []uint64{1, 2, 3, 4, 5},
			b: []uint64{6, 7, 8, 9, 10},

			seek:    2,
			success: true,
			res:     []uint64{2, 3, 4, 5},
		},
		{
			a: []uint64{1, 2, 3, 4, 5},
			b: []uint64{4, 5, 6, 7, 8},

			seek:    9,
			success: false,
			res:     nil,
		},
		{
			a: []uint64{1, 2, 3, 4, 9, 10},
			b: []uint64{1, 4, 5, 6, 7, 8, 10, 11},

			seek:    10,
			success: false,
			res:     nil,
		},
		{
			a: []uint64{1, 2, 3, 4, 9, 10},
			b: []uint64{1, 4, 5, 6, 7, 8, 11},

			seek:    4,
			success: true,
			res:     []uint64{9, 10},
		},
		{
			a: []uint64{1, 2, 3, 4, 9, 10},
			b: []uint64{1, 4, 5, 6, 7, 8, 11},

			seek:    5,
			success: true,
			res:     []uint64{9, 10},
		},
		{
			a: []uint64{1, 2, 3, 4, 9, 10},
			b: []uint64{1, 4, 5, 6, 7, 8, 11},

			seek:    10,
			success: true,
			res:     []uint64{10},
		},
	}

	for _, c := range cases {
628 629
		a := newListPostings(c.a...)
		b := newListPostings(c.b...)
630 631 632 633

		p := newRemovedPostings(a, b)

		testutil.Equals(t, c.success, p.Seek(c.seek))
634 635 636 637

		// After Seek(), At() should be called.
		if c.success {
			start := p.At()
638
			lst, err := ExpandPostings(p)
C
Callum Styan 已提交
639
			testutil.Ok(t, err)
640

641
			lst = append([]uint64{start}, lst...)
C
Callum Styan 已提交
642
			testutil.Equals(t, c.res, lst)
643
		}
F
Fabian Reinartz 已提交
644 645
	}
}
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

func TestBigEndian(t *testing.T) {
	num := 1000
	// mock a list as postings
	ls := make([]uint32, num)
	ls[0] = 2
	for i := 1; i < num; i++ {
		ls[i] = ls[i-1] + uint32(rand.Int31n(25)) + 2
	}

	beLst := make([]byte, num*4)
	for i := 0; i < num; i++ {
		b := beLst[i*4 : i*4+4]
		binary.BigEndian.PutUint32(b, ls[i])
	}

	t.Run("Iteration", func(t *testing.T) {
		bep := newBigEndianPostings(beLst)
		for i := 0; i < num; i++ {
C
Callum Styan 已提交
665 666
			testutil.Assert(t, bep.Next() == true, "")
			testutil.Equals(t, uint64(ls[i]), bep.At())
667 668
		}

C
Callum Styan 已提交
669
		testutil.Assert(t, bep.Next() == false, "")
C
Callum Styan 已提交
670
		testutil.Assert(t, bep.Err() == nil, "")
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	})

	t.Run("Seek", func(t *testing.T) {
		table := []struct {
			seek  uint32
			val   uint32
			found bool
		}{
			{
				ls[0] - 1, ls[0], true,
			},
			{
				ls[4], ls[4], true,
			},
			{
				ls[500] - 1, ls[500], true,
			},
			{
				ls[600] + 1, ls[601], true,
			},
			{
692
				ls[600] + 1, ls[601], true,
693 694
			},
			{
695
				ls[600] + 1, ls[601], true,
696 697
			},
			{
698
				ls[0], ls[601], true,
699 700
			},
			{
701
				ls[600], ls[601], true,
702 703 704 705 706 707 708 709 710 711 712 713
			},
			{
				ls[999], ls[999], true,
			},
			{
				ls[999] + 10, ls[999], false,
			},
		}

		bep := newBigEndianPostings(beLst)

		for _, v := range table {
C
Callum Styan 已提交
714 715
			testutil.Equals(t, v.found, bep.Seek(uint64(v.seek)))
			testutil.Equals(t, uint64(v.val), bep.At())
C
Callum Styan 已提交
716
			testutil.Assert(t, bep.Err() == nil, "")
717 718 719
		}
	})
}
720 721

func TestIntersectWithMerge(t *testing.T) {
722
	// One of the reproducible cases for:
723
	// https://github.com/prometheus/prometheus/issues/2616
724
	a := newListPostings(21, 22, 23, 24, 25, 30)
725

726
	b := Merge(
727 728
		newListPostings(10, 20, 30),
		newListPostings(15, 26, 30),
729 730 731
	)

	p := Intersect(a, b)
732
	res, err := ExpandPostings(p)
733

C
Callum Styan 已提交
734 735
	testutil.Ok(t, err)
	testutil.Equals(t, []uint64{30}, res)
736
}
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

func TestWithoutPostings(t *testing.T) {
	var cases = []struct {
		base Postings
		drop Postings

		res Postings
	}{
		{
			base: EmptyPostings(),
			drop: EmptyPostings(),

			res: EmptyPostings(),
		},
		{
			base: EmptyPostings(),
			drop: newListPostings(1, 2),

			res: EmptyPostings(),
		},
		{
			base: newListPostings(1, 2),
			drop: EmptyPostings(),

			res: newListPostings(1, 2),
		},
		{
			base: newListPostings(),
			drop: newListPostings(),

			res: newListPostings(),
		},
		{
			base: newListPostings(1, 2, 3),
			drop: newListPostings(),

			res: newListPostings(1, 2, 3),
		},
		{
			base: newListPostings(1, 2, 3),
			drop: newListPostings(4, 5, 6),

			res: newListPostings(1, 2, 3),
		},
		{
			base: newListPostings(1, 2, 3),
			drop: newListPostings(3, 4, 5),

			res: newListPostings(1, 2),
		},
	}

	for _, c := range cases {
		t.Run("", func(t *testing.T) {
			if c.res == nil {
				t.Fatal("without result expectancy cannot be nil")
			}

			expected, err := ExpandPostings(c.res)
			testutil.Ok(t, err)

			w := Without(c.base, c.drop)

			if c.res == EmptyPostings() {
				testutil.Equals(t, EmptyPostings(), w)
				return
			}

			if w == EmptyPostings() {
				t.Fatal("without unexpected result: EmptyPostings sentinel")
			}

			res, err := ExpandPostings(w)
			testutil.Ok(t, err)
			testutil.Equals(t, expected, res)
		})
	}
}