24 import java.io.PrintWriter;
25 import java.nio.ByteBuffer;
26 import java.nio.charset.Charset;
28 import com.mysql.ndbjtie.mysql.Utils;
36 public void test_s2b2s(
String s,
int prec,
int scale) {
37 out.print(
" [" + prec +
"/" + scale +
"] '" + s +
"' => ");
41 final ByteBuffer sbuf = ByteBuffer.allocateDirect(128);
44 b = s.getBytes(
"US-ASCII");
45 }
catch (java.io.UnsupportedEncodingException ex) {
46 throw new RuntimeException(ex);
48 assert (s.equals(
new String(b)));
49 assert (b.length < sbuf.capacity());
51 assert (b.length == sbuf.position());
53 assert (b.length == sbuf.limit());
56 final ByteBuffer bbuf = ByteBuffer.allocateDirect(128);
58 for (
int i = 0;
i < bbuf.capacity();
i++) bbuf.put((byte)0);
62 assert (sbuf.position() == 0);
63 assert (bbuf.position() == 0);
64 final int r1 = Utils.decimal_str2bin(sbuf, sbuf.capacity(),
66 bbuf, bbuf.capacity());
67 if (r1 != Utils.E_DEC_OK) {
68 out.println(
"decimal_str2bin() returned: " + r1);
74 for (
int i = 0;
i < sbuf.capacity();
i++) sbuf.put((byte)0);
78 assert (bbuf.position() == 0);
79 assert (sbuf.position() == 0);
80 final int r2 = Utils.decimal_bin2str(bbuf, bbuf.capacity(),
82 sbuf, sbuf.capacity());
83 if (r2 != Utils.E_DEC_OK) {
84 out.println(
"decimal_bin2str() returned: " + r2);
89 assert (sbuf.position() == 0);
91 final String t = Charset.forName(
"US-ASCII").decode(sbuf).toString();
93 out.println(
"'" + t +
"'");
97 out.println(
"--> MySqlUtilsDecimalTest.test()");
102 test_s2b2s(
"3.3", 2, 1);
103 test_s2b2s(
"124.000", 20, 4);
104 test_s2b2s(
"-11", 14, 1);
105 test_s2b2s(
"1.123456000000000", 20, 16);
106 test_s2b2s(
"0", 20, 10);
107 test_s2b2s(
"1 ", 20, 10);
108 test_s2b2s(
"1,35", 20, 10);
109 test_s2b2s(
"text",20, 10);
112 out.println(
"<-- MySqlUtilsDecimalTest.test()");
115 static public void main(
String[] args)
throws Exception {
116 out.println(
"--> MySqlUtilsDecimalTest.main()");
123 out.println(
"<-- MySqlUtilsDecimalTest.main()");