File: testutil_test.go

package info (click to toggle)
golang-github-efficientgo-core 1.0.0~rc3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 580 kB
  • sloc: makefile: 68
file content (126 lines) | stat: -rw-r--r-- 2,801 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.

package testutil

import (
	"math"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func TestContains(t *testing.T) {
	tests := map[string]struct {
		haystack    []string
		needle      []string
		shouldMatch bool
	}{
		"empty haystack": {
			haystack:    []string{},
			needle:      []string{"key1"},
			shouldMatch: false,
		},

		"empty needle": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{},
			shouldMatch: false,
		},

		"single value needle": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key1"},
			shouldMatch: true,
		},

		"multiple value needle": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key1", "key2"},
			shouldMatch: true,
		},

		"same size needle as haystack": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key1", "key2", "key3"},
			shouldMatch: true,
		},

		"larger needle than haystack": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key1", "key2", "key3", "key4"},
			shouldMatch: false,
		},

		"needle not contained": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key4"},
			shouldMatch: false,
		},

		"haystack ends before needle": {
			haystack:    []string{"key1", "key2", "key3"},
			needle:      []string{"key3", "key4"},
			shouldMatch: false,
		},
	}

	for testName, testData := range tests {
		t.Run(testName, func(t *testing.T) {
			if testData.shouldMatch != contains(testData.haystack, testData.needle) {
				t.Fatalf("unexpected result testing contains() with %#v", testData)
			}
		})
	}
}

type child struct {
	Val float64
}

type parent struct {
	C child
}

type unexp struct {
	val float64
}

func TestEqualsWithNaN(t *testing.T) {
	for _, tc := range []struct {
		name string
		a    interface{}
		b    interface{}
		opts cmp.Options
	}{
		{
			a:    math.NaN(),
			b:    math.NaN(),
			name: "Simple NaN value comparison",
			opts: cmp.Options{cmpopts.EquateNaNs()},
		},
		{
			a:    child{Val: math.NaN()},
			b:    child{Val: math.NaN()},
			name: "NaN value as struct member comparison",
			opts: cmp.Options{cmpopts.EquateNaNs()},
		},
		{
			a:    parent{C: child{Val: math.NaN()}},
			b:    parent{C: child{Val: math.NaN()}},
			name: "NaN value in nested struct comparison",
			opts: cmp.Options{cmpopts.EquateNaNs()},
		},
		{
			a:    unexp{val: math.NaN()},
			b:    unexp{val: math.NaN()},
			name: "NaN value as unexported struct member comparison",
			opts: cmp.Options{cmp.AllowUnexported(unexp{}), cmpopts.EquateNaNs()},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			WithGoCmp(tc.opts).Equals(t, tc.a, tc.b)
		})
	}
}