File: query_test.go

package info (click to toggle)
golang-github-marstr-collection 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,069 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package collection

import (
	"context"
	"testing"
	"time"
)

func Test_Empty(t *testing.T) {
	if Any(Empty[int]()) {
		t.Log("empty should not have any elements")
		t.Fail()
	}

	if CountAll(Empty[int]()) != 0 {
		t.Log("empty should have counted to zero elements")
		t.Fail()
	}

	alwaysTrue := func(x int) bool {
		return true
	}

	if Count(Empty[int](), alwaysTrue) != 0 {
		t.Log("empty should have counted to zero even when discriminating")
		t.Fail()
	}
}

func BenchmarkEnumerator_Sum(b *testing.B) {
	var nums EnumerableSlice[int] = getInitializedSequentialArray[int]()
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		slowNums := Select[int, int](nums, sleepIdentity[int])
		for range slowNums.Enumerate(ctx) {
			// Intentionally Left Blank
		}
	}
}

func sleepIdentity[T any](val T) T {
	time.Sleep(2 * time.Millisecond)
	return val
}

func getInitializedSequentialArray[T ~int]() []T {
	rawNums := make([]T, 1000)
	for i := range rawNums {
		rawNums[i] = T(i + 1)
	}
	return rawNums
}