More tests for StringBuilder.append(D)

This commit is contained in:
konsoletyper 2013-12-20 13:19:00 +04:00
parent 111b3d9076
commit 97107a2953
3 changed files with 76 additions and 9 deletions

View File

@ -243,7 +243,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
// Extend buffer to store exponent // Extend buffer to store exponent
if (exp != 0) { if (exp != 0) {
sz += 2; sz += 2;
if (exp < 10 || exp > 10) { if (exp <= -10 || exp >= 10) {
++sz; ++sz;
} }
} }
@ -276,7 +276,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = -exp; exp = -exp;
buffer[length++] = '-'; buffer[length++] = '-';
} }
if (exp > 10) { if (exp >= 10) {
buffer[length++] = (char)('0' + exp / 10); buffer[length++] = (char)('0' + exp / 10);
} }
buffer[length++] = (char)('0' + exp % 10); buffer[length++] = (char)('0' + exp % 10);
@ -354,13 +354,13 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
double digit = 1; double digit = 1;
for (int i = negDoublePowersOfTen.length - 1; i >= 0; --i) { for (int i = negDoublePowersOfTen.length - 1; i >= 0; --i) {
if ((exp | bit) <= DOUBLE_MAX_EXPONENT && negDoublePowersOfTen[i] * digit * 10 > value) { if ((exp | bit) <= DOUBLE_MAX_EXPONENT && negDoublePowersOfTen[i] * digit * 10 > value) {
digit *= negPowersOfTen[i]; digit *= negDoublePowersOfTen[i];
exp |= bit; exp |= bit;
} }
bit >>= 1; bit >>= 1;
} }
exp = -exp; exp = -exp;
mantissa = (long)(((value * DOUBLE_MAX_POS) / digit) + 0.5f); mantissa = (long)(((value * DOUBLE_MAX_POS) / digit) + 0.5);
} }
// Remove trailing zeros // Remove trailing zeros
@ -387,10 +387,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
// Extend buffer to store exponent // Extend buffer to store exponent
if (exp != 0) { if (exp != 0) {
sz += 2; sz += 2;
if (exp < 10 || exp > 10) { if (exp <= -10 || exp >= 10) {
++sz; ++sz;
} }
if (exp < 100 || exp > 100) { if (exp <= -100 || exp >= 100) {
++sz; ++sz;
} }
} }
@ -423,11 +423,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = -exp; exp = -exp;
buffer[length++] = '-'; buffer[length++] = '-';
} }
if (exp > 100) { if (exp >= 100) {
buffer[length++] = (char)('0' + exp / 100); buffer[length++] = (char)('0' + exp / 100);
exp %= 100; exp %= 100;
} buffer[length++] = (char)('0' + exp / 10);
if (exp > 10) { } else if (exp >= 10) {
buffer[length++] = (char)('0' + exp / 10); buffer[length++] = (char)('0' + exp / 10);
} }
buffer[length++] = (char)('0' + exp % 10); buffer[length++] = (char)('0' + exp % 10);

View File

@ -425,6 +425,10 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return new TStringBuilder().append(f).toString0(); return new TStringBuilder().append(f).toString0();
} }
public static TString valueOf(double d) {
return new TStringBuilder().append(d).toString0();
}
@Override @Override
public boolean equals(TObject other) { public boolean equals(TObject other) {
if (this == other) { if (this == other) {

View File

@ -212,6 +212,69 @@ public class StringBuilderTest {
assertEquals("1.23456789E150", sb.toString()); assertEquals("1.23456789E150", sb.toString());
} }
@Test
public void negativeDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(-1.23456789E150);
assertEquals("-1.23456789E150", sb.toString());
}
@Test
public void smallDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(1.23456789E-150);
assertEquals("1.23456789E-150", sb.toString());
}
@Test
public void maxDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(1.79769313486231E308);
assertEquals("1.79769313486231E308", sb.toString());
}
@Test
public void minDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(2.2250738585072E-308);
assertEquals("2.2250738585072E-308", sb.toString());
}
@Test
public void zeroDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(0);
assertEquals("0", sb.toString());
}
@Test
public void doubleInfinityAppended() {
StringBuilder sb = new StringBuilder();
sb.append(Double.POSITIVE_INFINITY);
assertEquals("Infinity", sb.toString());
}
@Test
public void doubleNaNAppended() {
StringBuilder sb = new StringBuilder();
sb.append(Double.NaN);
assertEquals("NaN", sb.toString());
}
@Test
public void normalDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(1200.0);
assertEquals("1200.0", sb.toString());
}
@Test
public void normalSmallDoubleAppended() {
StringBuilder sb = new StringBuilder();
sb.append(0.023);
assertEquals("0.023", sb.toString());
}
@Test @Test
public void appendsCodePoint() { public void appendsCodePoint() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();