/
TestNumberPrinting.java
113 lines (98 loc) · 3.42 KB
/
TestNumberPrinting.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
package tools.jackson.core.util;
import java.util.Random;
import tools.jackson.core.io.NumberOutput;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Set of basic unit tests for verifying that the low-level number
* printing methods work as expected.
*/
class TestNumberPrinting
extends tools.jackson.core.JUnit5TestBase
{
@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);
}
}
/*
/**********************************************************
/* 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 = Integer.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 = Long.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);
}
}