/
NumberOutputTest.java
149 lines (131 loc) · 4.68 KB
/
NumberOutputTest.java
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
package com.fasterxml.jackson.core.io;
import java.util.Random;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
class NumberOutputTest
{
@Test
void intPrinting() throws Exception
{
assertIntPrint(0);
assertIntPrint(-3);
assertIntPrint(1234);
assertIntPrint(-1234);
assertIntPrint(56789);
assertIntPrint(-56789);
assertIntPrint(999999);
assertIntPrint(-999999);
assertIntPrint(1000000);
assertIntPrint(-1000000);
assertIntPrint(10000001);
assertIntPrint(-10000001);
assertIntPrint(-100000012);
assertIntPrint(100000012);
assertIntPrint(1999888777);
assertIntPrint(-1999888777);
assertIntPrint(Integer.MAX_VALUE);
assertIntPrint(Integer.MIN_VALUE);
Random rnd = new Random(12345L);
for (int i = 0; i < 251000; ++i) {
assertIntPrint(rnd.nextInt());
}
}
@Test
void longPrinting() throws Exception
{
// First, let's just cover couple of edge cases
assertLongPrint(0L, 0);
assertLongPrint(1L, 0);
assertLongPrint(-1L, 0);
assertLongPrint(Long.MAX_VALUE, 0);
assertLongPrint(Long.MIN_VALUE, 0);
assertLongPrint(Long.MAX_VALUE-1L, 0);
assertLongPrint(Long.MIN_VALUE+1L, 0);
Random rnd = new Random(12345L);
// Bigger value space, need more iterations for long
for (int i = 0; i < 678000; ++i) {
long l = ((long) rnd.nextInt() << 32) | rnd.nextInt();
assertLongPrint(l, i);
}
}
// // // Tests for divBy1000
@Test
void divBy1000Small()
{
for (int number = 0; number <= 999_999; ++number) {
int expected = number / 1000;
int actual = NumberOutput.divBy1000(number);
if (expected != actual) { // only construct String if fail
fail("With "+number+" should get "+expected+", got: "+actual);
}
}
}
@Test
void divBy1000Sampled()
{
for (int number = 1_000_000; number > 0; number += 7) {
int expected = number / 1000;
int actual = NumberOutput.divBy1000(number);
if (expected != actual) { // only construct String if fail
fail("With "+number+" should get "+expected+", got: "+actual);
}
}
}
// And then full range, not included in CI since code shouldn't change;
// but has been run to verify full range manually
@Test
// Comment out for manual testing:
@Disabled
void divBy1000FullRange() {
for (int number = 0; number <= Integer.MAX_VALUE; ++number) {
int expected = number / 1000;
int actual = NumberOutput.divBy1000(number);
if (expected != actual) { // only construct String if fail
fail("With "+number+" should get "+expected+", got: "+actual);
}
}
}
/*
/**********************************************************
/* Internal methods
/**********************************************************
*/
private void assertIntPrint(int value)
{
String exp = ""+value;
String act = printToString(value);
if (!exp.equals(act)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")");
}
String alt = NumberOutput.toString(value);
if (!exp.equals(alt)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")");
}
}
private void assertLongPrint(long value, int index)
{
String exp = ""+value;
String act = printToString(value);
if (!exp.equals(act)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")");
}
String alt = NumberOutput.toString(value);
if (!exp.equals(alt)) {
assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")");
}
}
private String printToString(int value)
{
char[] buffer = new char[12];
int offset = NumberOutput.outputInt(value, buffer, 0);
return new String(buffer, 0, offset);
}
private String printToString(long value)
{
char[] buffer = new char[22];
int offset = NumberOutput.outputLong(value, buffer, 0);
return new String(buffer, 0, offset);
}
}