forked from xeipuuv/gojsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
91 lines (78 loc) · 3.13 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
// 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 (
"encoding/json"
"github.com/stretchr/testify/assert"
"math"
"testing"
)
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))
}
func TestCheckJsonNumber(t *testing.T) {
var testCases = []struct {
isInt bool
value json.Number
}{
{true, "0"},
{true, "2147483647"},
{true, "-2147483648"},
{true, "9223372036854775807"},
{true, "-9223372036854775808"},
{true, "1.0e+2"},
{true, "1.0e+10"},
{true, "-1.0e+2"},
{true, "-1.0e+10"},
{false, "1.0e-2"},
{false, "number"},
{false, "123number"},
}
for _, testCase := range testCases {
assert.Equal(t, testCase.isInt, checkJsonInteger(testCase.value))
assert.Equal(t, testCase.isInt, checkJsonInteger(testCase.value))
}
}