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
|
/*
* Copyright (c) 2014 Google, Inc.
*
* 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.
*/
package com.google.common.truth;
import static com.google.common.truth.MathUtil.equalWithinTolerance;
import static com.google.common.truth.MathUtil.notEqualWithinTolerance;
import static com.google.common.truth.Truth.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Tests for {@link MathUtil} used by numeric subjects.
*
* @author Christian Gruber (cgruber@israfil.net)
*/
@RunWith(JUnit4.class)
public class MathUtilTest {
@Test
public void floatEquals() {
assertThat(equalWithinTolerance(1.3f, 1.3f, 0.00000000000001f)).isTrue();
assertThat(equalWithinTolerance(1.3f, 1.3f, 0.0f)).isTrue();
assertThat(equalWithinTolerance(0.0f, 1.0f + 2.0f - 3.0f, 0.00000000000000000000000000000001f))
.isTrue();
assertThat(equalWithinTolerance(1.3f, 1.303f, 0.004f)).isTrue();
assertThat(equalWithinTolerance(1.3f, 1.303f, 0.002f)).isFalse();
assertThat(equalWithinTolerance(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 0.01f))
.isFalse();
assertThat(equalWithinTolerance(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.01f))
.isFalse();
assertThat(equalWithinTolerance(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.01f))
.isFalse();
assertThat(equalWithinTolerance(Float.NaN, Float.NaN, 0.01f)).isFalse();
}
@Test
public void doubleEquals() {
assertThat(equalWithinTolerance(1.3d, 1.3d, 0.00000000000001d)).isTrue();
assertThat(equalWithinTolerance(1.3d, 1.3d, 0.0d)).isTrue();
assertThat(equalWithinTolerance(0.0d, 1.0d + 2.0d - 3.0d, 0.00000000000000000000000000000001d))
.isTrue();
assertThat(equalWithinTolerance(1.3d, 1.303d, 0.004d)).isTrue();
assertThat(equalWithinTolerance(1.3d, 1.303d, 0.002d)).isFalse();
assertThat(equalWithinTolerance(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0.01d))
.isFalse();
assertThat(equalWithinTolerance(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.01d))
.isFalse();
assertThat(equalWithinTolerance(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.01d))
.isFalse();
assertThat(equalWithinTolerance(Double.NaN, Double.NaN, 0.01d)).isFalse();
}
@Test
public void floatNotEquals() {
assertThat(notEqualWithinTolerance(1.3f, 1.3f, 0.00000000000001f)).isFalse();
assertThat(notEqualWithinTolerance(1.3f, 1.3f, 0.0f)).isFalse();
assertThat(
notEqualWithinTolerance(0.0f, 1.0f + 2.0f - 3.0f, 0.00000000000000000000000000000001f))
.isFalse();
assertThat(notEqualWithinTolerance(1.3f, 1.303f, 0.004f)).isFalse();
assertThat(notEqualWithinTolerance(1.3f, 1.303f, 0.002f)).isTrue();
assertThat(notEqualWithinTolerance(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 0.01f))
.isFalse();
assertThat(notEqualWithinTolerance(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.01f))
.isFalse();
assertThat(notEqualWithinTolerance(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.01f))
.isFalse();
assertThat(notEqualWithinTolerance(Float.NaN, Float.NaN, 0.01f)).isFalse();
}
@Test
public void doubleNotEquals() {
assertThat(notEqualWithinTolerance(1.3d, 1.3d, 0.00000000000001d)).isFalse();
assertThat(notEqualWithinTolerance(1.3d, 1.3d, 0.0d)).isFalse();
assertThat(
notEqualWithinTolerance(0.0d, 1.0d + 2.0d - 3.0d, 0.00000000000000000000000000000001d))
.isFalse();
assertThat(notEqualWithinTolerance(1.3d, 1.303d, 0.004d)).isFalse();
assertThat(notEqualWithinTolerance(1.3d, 1.303d, 0.002d)).isTrue();
assertThat(notEqualWithinTolerance(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0.01d))
.isFalse();
assertThat(notEqualWithinTolerance(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.01d))
.isFalse();
assertThat(notEqualWithinTolerance(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.01d))
.isFalse();
assertThat(notEqualWithinTolerance(Double.NaN, Double.NaN, 0.01d)).isFalse();
}
@Test
public void equalsDifferentTypes() {
assertThat(equalWithinTolerance(1.3d, 1.3f, 0.00000000000001d)).isFalse();
assertThat(equalWithinTolerance(1.3f, 1.3d, 0.00000000000001f)).isFalse();
}
// TODO(cgruber): More complicated ways to break float/double casting to make sure.
}
|