-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI16_test.go
152 lines (123 loc) · 3.31 KB
/
I16_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
145
146
147
148
149
150
151
152
// DO NOT EDIT! This file was generated via `go generate`
// Copyright 2017 by David A. Golden. All rights reserved.
//
// 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
package listy_test
import (
"encoding/json"
"testing"
"github.com/xdg/listy"
"github.com/xdg/testy"
)
func getI16TestData(is *testy.T, v interface{}) {
err := json.Unmarshal(testdata["ints.json"], v)
if err != nil {
is.Fatalf("Error unmarshaling ints.json: %s", err)
}
}
func TestListI16Box(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Box struct {
Concat []int16
Elem3 int16
Head int16
Init []int16
Input []int16
Last int16
Len int
Less23 bool
Less32 bool
Reverse []int16
Swap []int16
Tail []int16
Unbox []int16
}
}
getI16TestData(is, &data)
xs := listy.I16(data.Box.Input)
// Tests that return ints, bools, etc. irrespective of value type
is.Equal(xs.Len(), data.Box.Len)
is.Equal(xs.Less(2, 3), data.Box.Less23)
is.Equal(xs.Less(3, 2), data.Box.Less32)
// Tests that return the value type
is.Equal(xs.Head(), data.Box.Head)
is.Equal(xs.Elem(3), data.Box.Elem3)
is.Equal(xs.Last(), data.Box.Last)
// Tests that return a slice of the value type
is.Equal(xs.Unbox(), data.Box.Unbox)
is.Equal(xs.Tail().Unbox(), data.Box.Tail)
is.Equal(xs.Init().Unbox(), data.Box.Init)
is.Equal(xs.Concat(data.Box.Input).Unbox(), data.Box.Concat)
is.Equal(xs.ConcatI16(listy.I16(data.Box.Input)).Unbox(), data.Box.Concat)
is.Equal(xs.Reverse().Unbox(), data.Box.Reverse)
// For Sort.Interface, Swap is in place and returns nothing
xs = listy.I16(data.Box.Input)
xs.Swap(0, 1)
is.Equal(xs.Unbox(), data.Box.Swap)
}
func TestListI16Contains(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Contains struct {
Input []int16
ContainsTrue []int16
ContainsFalse []int16
}
}
getI16TestData(is, &data)
xs := listy.I16(data.Contains.Input)
for _, x := range data.Contains.ContainsTrue {
is.True(xs.Contains(x))
}
for _, x := range data.Contains.ContainsFalse {
is.False(xs.Contains(x))
}
}
func TestListI16Filter(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Filter struct {
Input []int16
Lessthan int16
Filtered []int16
}
}
getI16TestData(is, &data)
xs := listy.I16(data.Filter.Input)
ys := xs.Filter(func(v int16) bool { return v < data.Filter.Lessthan })
is.Equal(ys.Unbox(), data.Filter.Filtered)
}
func TestListI16Map(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Map struct {
Input []int16
Add int16
Mapped []int16
}
}
getI16TestData(is, &data)
xs := listy.I16(data.Map.Input)
ys := xs.Map(func(v int16) int16 { return v + data.Map.Add })
is.Equal(ys.Unbox(), data.Map.Mapped)
}
func TestListI16Uniq(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Uniq struct {
Cases map[string][2][]int16
}
}
getI16TestData(is, &data)
for k, v := range data.Uniq.Cases {
is.Label(k).Equal(listy.I16(v[0]).Uniq().Unbox(), v[1])
}
}