-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from olyutorskii/release/v1.103.2
Release/v1.103.2
- Loading branch information
Showing
7 changed files
with
315 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* License : The MIT License | ||
* Copyright(c) 2017 olyutorskii | ||
*/ | ||
|
||
package io.github.olyutorskii.doubdabc.io; | ||
|
||
import io.github.olyutorskii.doubdabc.BcdRegister; | ||
import io.github.olyutorskii.doubdabc.BitFeeder; | ||
import io.github.olyutorskii.doubdabc.DecimalOut; | ||
import java.io.FilterWriter; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
/** | ||
* Decimal-form-integer writer with Double-Dabble algorithm. | ||
* | ||
* <p>It's similar to {@link java.io.PrintWriter} | ||
* | ||
* @see java.io.PrintWriter | ||
*/ | ||
public class DecimalWriter extends FilterWriter{ | ||
|
||
private final BcdRegister bcd; | ||
private final DecimalOut decOut; | ||
|
||
|
||
/** | ||
* Constructor. | ||
* @param out char output stream | ||
*/ | ||
public DecimalWriter(Writer out){ | ||
super(out); | ||
|
||
this.bcd = new BcdRegister(BcdRegister.MAX_COL_UINT64); | ||
this.decOut = new DecimalOut(this.bcd); | ||
|
||
return; | ||
} | ||
|
||
|
||
/** | ||
* Print signed int value. | ||
* | ||
* <p>It's similar to {@link java.io.PrintWriter#print(int)}. | ||
* | ||
* @param iVal int value | ||
* @throws IOException output error | ||
* @see java.io.PrintWriter#print(int) | ||
*/ | ||
public void print(int iVal) throws IOException{ | ||
boolean negSign; | ||
int absVal; | ||
|
||
if(iVal < 0){ | ||
negSign = true; | ||
absVal = -iVal; | ||
// -2147483648 does not change. OK, NP. | ||
}else{ | ||
negSign = false; | ||
absVal = iVal; | ||
} | ||
|
||
synchronized(this.bcd){ | ||
BitFeeder.feedUInt32(this.bcd, absVal); | ||
|
||
synchronized(this.lock){ | ||
if(negSign) append('-'); | ||
this.decOut.writeDigits(this); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
/** | ||
* Print signed long value. | ||
* | ||
* <p>It's similar to {@link java.io.PrintWriter#print(long)}. | ||
* | ||
* @param lVal long value | ||
* @throws IOException output error | ||
* @see java.io.PrintWriter#print(long) | ||
*/ | ||
public void print(long lVal) throws IOException{ | ||
boolean negSign; | ||
long absVal; | ||
|
||
if(lVal < 0){ | ||
negSign = true; | ||
absVal = -lVal; | ||
// -9223372036854775808 does not change. OK, NP. | ||
}else{ | ||
negSign = false; | ||
absVal = lVal; | ||
} | ||
|
||
synchronized(this.bcd){ | ||
BitFeeder.feedUInt64(this.bcd, absVal); | ||
|
||
synchronized(this.lock){ | ||
if(negSign) append('-'); | ||
this.decOut.writeDigits(this); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* License : The MIT License | ||
* Copyright(c) 2017 olyutorskii | ||
*/ | ||
|
||
/** | ||
* Extended I/O-classes with Double-Dabble algorithm. | ||
*/ | ||
|
||
package io.github.olyutorskii.doubdabc.io; | ||
|
||
/* EOF */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* License : The MIT License | ||
* Copyright(c) 2017 olyutorskii | ||
*/ | ||
|
||
package io.github.olyutorskii.doubdabc.io; | ||
|
||
import java.io.CharArrayWriter; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* | ||
*/ | ||
public class DecimalWriterTest { | ||
|
||
public DecimalWriterTest() { | ||
} | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
} | ||
|
||
/** | ||
* Test of print method, of class DecimalWriter. | ||
*/ | ||
@Test | ||
public void testPrint_int() throws Exception { | ||
System.out.println("print"); | ||
|
||
DecimalWriter writer; | ||
CharArrayWriter out; | ||
|
||
out = new CharArrayWriter(); | ||
writer = new DecimalWriter(out); | ||
|
||
out.reset(); | ||
writer.print(1); | ||
assertEquals("1", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(0); | ||
assertEquals("0", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-1); | ||
assertEquals("-1", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(999); | ||
assertEquals("999", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-999); | ||
assertEquals("-999", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(1_234_567_890); | ||
assertEquals("1234567890", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-1_234_567_890); | ||
assertEquals("-1234567890", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Integer.MAX_VALUE - 1); | ||
assertEquals("2147483646", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Integer.MAX_VALUE); | ||
assertEquals("2147483647", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Integer.MIN_VALUE); | ||
assertEquals("-2147483648", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Integer.MIN_VALUE + 1); | ||
assertEquals("-2147483647", out.toString()); | ||
|
||
return; | ||
} | ||
|
||
/** | ||
* Test of print method, of class DecimalWriter. | ||
*/ | ||
@Test | ||
public void testPrint_long() throws Exception { | ||
System.out.println("print"); | ||
|
||
DecimalWriter writer; | ||
CharArrayWriter out; | ||
|
||
out = new CharArrayWriter(); | ||
writer = new DecimalWriter(out); | ||
|
||
out.reset(); | ||
writer.print(1L); | ||
assertEquals("1", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(0L); | ||
assertEquals("0", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-1L); | ||
assertEquals("-1", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(999L); | ||
assertEquals("999", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-999L); | ||
assertEquals("-999", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(1_234_567_890L); | ||
assertEquals("1234567890", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(-1_234_567_890L); | ||
assertEquals("-1234567890", out.toString()); | ||
|
||
out.reset(); | ||
writer.print((long)Integer.MAX_VALUE); | ||
assertEquals("2147483647", out.toString()); | ||
|
||
out.reset(); | ||
writer.print((long)Integer.MAX_VALUE + 1L); | ||
assertEquals("2147483648", out.toString()); | ||
|
||
out.reset(); | ||
writer.print((long)Integer.MIN_VALUE); | ||
assertEquals("-2147483648", out.toString()); | ||
|
||
out.reset(); | ||
writer.print((long)Integer.MIN_VALUE - 1L); | ||
assertEquals("-2147483649", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Long.MAX_VALUE - 1L); | ||
assertEquals("9223372036854775806", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Long.MAX_VALUE); | ||
assertEquals("9223372036854775807", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Long.MIN_VALUE); | ||
assertEquals("-9223372036854775808", out.toString()); | ||
|
||
out.reset(); | ||
writer.print(Long.MIN_VALUE + 1L); | ||
assertEquals("-9223372036854775807", out.toString()); | ||
|
||
return; | ||
} | ||
|
||
} |