This repository was archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_test.go
144 lines (131 loc) · 3.06 KB
/
float_test.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 Romano de Souza. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package coalesce_test
import (
"reflect"
"testing"
"github.com/romanodesouza/coalesce"
)
func TestFloat32(t *testing.T) {
tests := []struct {
name string
in []*float32
want *float32
}{
{
name: "it should return the first non-nil value",
in: []*float32{nil, ptrfloat32(1), ptrfloat32(2)},
want: ptrfloat32(1),
},
{
name: "it should return nil if a non-nil value has not been found",
in: []*float32{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Float32(tt.in...)
if tt.want != nil && *got != *tt.want {
t.Errorf("got %v; want %v", *got, *tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", *got, tt.want)
}
})
}
}
func TestFloat32Slice(t *testing.T) {
tests := []struct {
name string
in [][]float32
want []float32
}{
{
name: "it should return the first non-nil value",
in: [][]float32{nil, []float32{1}, []float32{2}},
want: []float32{1},
},
{
name: "it should return nil if a non-nil value has not been found",
in: [][]float32{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Float32Slice(tt.in...)
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v; want %v", got, tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}
func TestFloat64(t *testing.T) {
tests := []struct {
name string
in []*float64
want *float64
}{
{
name: "it should return the first non-nil value",
in: []*float64{nil, ptrfloat64(1), ptrfloat64(2)},
want: ptrfloat64(1),
},
{
name: "it should return nil if a non-nil value has not been found",
in: []*float64{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Float64(tt.in...)
if tt.want != nil && *got != *tt.want {
t.Errorf("got %v; want %v", *got, *tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", *got, tt.want)
}
})
}
}
func TestFloat64Slice(t *testing.T) {
tests := []struct {
name string
in [][]float64
want []float64
}{
{
name: "it should return the first non-nil value",
in: [][]float64{nil, []float64{1}, []float64{2}},
want: []float64{1},
},
{
name: "it should return nil if a non-nil value has not been found",
in: [][]float64{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Float64Slice(tt.in...)
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v; want %v", got, tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}
func ptrfloat32(f float32) *float32 {
return &f
}
func ptrfloat64(f float64) *float64 {
return &f
}