forked from xeipuuv/gojsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
157 lines (135 loc) · 6.62 KB
/
utils_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
153
154
155
156
157
// Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
//
// 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.
// author janmentzel
// author-github https://github.com/janmentzel
// author-mail ? ( forward to [email protected] )
//
// repository-name gojsonschema
// repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
//
// description (Unit) Tests for utils ( Float / Integer conversion ).
//
// created 08-08-2013
package gojsonschema
import (
"github.com/stretchr/testify/assert"
"math"
"testing"
)
func TestIsFloat64IntegerA(t *testing.T) {
assert.False(t, isFloat64AnInteger(math.Inf(+1)))
assert.False(t, isFloat64AnInteger(math.Inf(-1)))
assert.False(t, isFloat64AnInteger(math.NaN()))
assert.False(t, isFloat64AnInteger(math.Float64frombits((1<<11-1)<<52|1)))
assert.True(t, isFloat64AnInteger(float64(0.0)))
assert.True(t, isFloat64AnInteger(-float64(0.0)))
assert.False(t, isFloat64AnInteger(float64(0.5)))
assert.True(t, isFloat64AnInteger(float64(1.0)))
assert.True(t, isFloat64AnInteger(-float64(1.0)))
assert.False(t, isFloat64AnInteger(float64(1.1)))
assert.True(t, isFloat64AnInteger(float64(131.0)))
assert.True(t, isFloat64AnInteger(-float64(131.0)))
assert.True(t, isFloat64AnInteger(float64(1<<52-1)))
assert.True(t, isFloat64AnInteger(-float64(1<<52-1)))
assert.True(t, isFloat64AnInteger(float64(1<<52)))
assert.True(t, isFloat64AnInteger(-float64(1<<52)))
assert.True(t, isFloat64AnInteger(float64(1<<53-1)))
assert.True(t, isFloat64AnInteger(-float64(1<<53-1)))
assert.True(t, isFloat64AnInteger(float64(1<<53-1)))
assert.False(t, isFloat64AnInteger(float64(1<<53)))
assert.False(t, isFloat64AnInteger(-float64(1<<53)))
assert.False(t, isFloat64AnInteger(float64(1<<63)))
assert.False(t, isFloat64AnInteger(-float64(1<<63)))
assert.False(t, isFloat64AnInteger(math.Nextafter(float64(1<<63), math.MaxFloat64)))
assert.False(t, isFloat64AnInteger(-math.Nextafter(float64(1<<63), math.MaxFloat64)))
assert.False(t, isFloat64AnInteger(float64(1<<70+3<<21)))
assert.False(t, isFloat64AnInteger(-float64(1<<70+3<<21)))
assert.False(t, isFloat64AnInteger(math.Nextafter(float64(9007199254740991.0), math.MaxFloat64)))
assert.True(t, isFloat64AnInteger(float64(9007199254740991.0)))
assert.True(t, isFloat64AnInteger(float64(-9007199254740991.0)))
assert.False(t, isFloat64AnInteger(math.Nextafter(float64(-9007199254740991.0), -math.MaxFloat64)))
}
func TestIsFloat64Integer(t *testing.T) {
// fails. MaxUint64 is to large for JS anyway, so we can ignore it here.
//assert.True(t, isFloat64AnInteger(float64(math.MaxUint64)))
assert.False(t, isFloat64AnInteger(math.MaxInt64))
assert.False(t, isFloat64AnInteger(1<<62))
assert.False(t, isFloat64AnInteger(math.MinInt64))
assert.True(t, isFloat64AnInteger(100100100100))
assert.True(t, isFloat64AnInteger(-100100100100))
assert.True(t, isFloat64AnInteger(100100100))
assert.True(t, isFloat64AnInteger(-100100100))
assert.True(t, isFloat64AnInteger(100100))
assert.True(t, isFloat64AnInteger(-100100))
assert.True(t, isFloat64AnInteger(100))
assert.True(t, isFloat64AnInteger(-100))
assert.True(t, isFloat64AnInteger(-0))
assert.True(t, isFloat64AnInteger(-1))
assert.True(t, isFloat64AnInteger(1))
assert.True(t, isFloat64AnInteger(0))
assert.True(t, isFloat64AnInteger(77))
assert.True(t, isFloat64AnInteger(-77))
assert.True(t, isFloat64AnInteger(1e10))
assert.True(t, isFloat64AnInteger(-1e10))
assert.True(t, isFloat64AnInteger(100100100.0))
assert.True(t, isFloat64AnInteger(-100100100.0))
assert.False(t, isFloat64AnInteger(100100100100.1))
assert.False(t, isFloat64AnInteger(-100100100100.1))
assert.False(t, isFloat64AnInteger(math.MaxFloat64))
assert.False(t, isFloat64AnInteger(-math.MaxFloat64))
assert.False(t, isFloat64AnInteger(1.1))
assert.False(t, isFloat64AnInteger(-1.1))
assert.False(t, isFloat64AnInteger(1.000000000001))
assert.False(t, isFloat64AnInteger(-1.000000000001))
assert.False(t, isFloat64AnInteger(1e-10))
assert.False(t, isFloat64AnInteger(-1e-10))
assert.False(t, isFloat64AnInteger(1.001))
assert.False(t, isFloat64AnInteger(-1.001))
assert.False(t, isFloat64AnInteger(0.0001))
assert.False(t, isFloat64AnInteger(1<<62))
assert.False(t, isFloat64AnInteger(math.MinInt64))
assert.False(t, isFloat64AnInteger(math.MaxInt64))
assert.False(t, isFloat64AnInteger(-1<<62))
assert.False(t, isFloat64AnInteger(1e-10))
assert.False(t, isFloat64AnInteger(-1e-10))
assert.False(t, isFloat64AnInteger(1.000000000001))
assert.False(t, isFloat64AnInteger(-1.000000000001))
assert.False(t, isFloat64AnInteger(4.611686018427387904e7))
assert.False(t, isFloat64AnInteger(-4.611686018427387904e7))
}
func TestResultErrorFormatNumber(t *testing.T) {
assert.Equal(t, "1", resultErrorFormatNumber(1))
assert.Equal(t, "-1", resultErrorFormatNumber(-1))
assert.Equal(t, "0", resultErrorFormatNumber(0))
// unfortunately, can not be recognized as float
assert.Equal(t, "0", resultErrorFormatNumber(0.0))
assert.Equal(t, "1.001", resultErrorFormatNumber(1.001))
assert.Equal(t, "-1.001", resultErrorFormatNumber(-1.001))
assert.Equal(t, "0.0001", resultErrorFormatNumber(0.0001))
// casting math.MaxInt64 (1<<63 -1) to float back to int64
// becomes negative. obviousely because of bit missinterpretation.
// so simply test a slightly smaller "large" integer here
assert.Equal(t, "4.611686018427388e+18", resultErrorFormatNumber(1<<62))
// with negative int64 max works
assert.Equal(t, "-9.223372036854776e+18", resultErrorFormatNumber(math.MinInt64))
assert.Equal(t, "-4.611686018427388e+18", resultErrorFormatNumber(-1<<62))
assert.Equal(t, "10000000000", resultErrorFormatNumber(1e10))
assert.Equal(t, "-10000000000", resultErrorFormatNumber(-1e10))
assert.Equal(t, "1.000000000001", resultErrorFormatNumber(1.000000000001))
assert.Equal(t, "-1.000000000001", resultErrorFormatNumber(-1.000000000001))
assert.Equal(t, "1e-10", resultErrorFormatNumber(1e-10))
assert.Equal(t, "-1e-10", resultErrorFormatNumber(-1e-10))
assert.Equal(t, "4.6116860184273876e+07", resultErrorFormatNumber(4.611686018427387904e7))
assert.Equal(t, "-4.6116860184273876e+07", resultErrorFormatNumber(-4.611686018427387904e7))
}