2
Copyright 2010 Sun Microsystems, Inc.
3
All rights reserved. Use is subject to license terms.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* MySqlUtilsDecimalTest.java
24
import java.io.PrintWriter;
25
import java.nio.ByteBuffer;
26
import java.nio.charset.Charset;
28
import com.mysql.ndbjtie.mysql.Utils;
31
* Tests the basic functioning of the NdbJTie libary: mysql decimal utils
32
* for string-binary conversions.
34
public class MySqlUtilsDecimalTest extends JTieTestBase {
36
public void test_s2b2s(String s, int prec, int scale) {
37
out.print(" [" + prec + "/" + scale + "] '" + s + "' => ");
40
// write string buffer
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());
55
// clear binary buffer
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);
72
// clear string buffer
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()");
99
// load native library
100
loadSystemLibrary("ndbclient");
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()");
119
MySqlUtilsDecimalTest test = new MySqlUtilsDecimalTest();
123
out.println("<-- MySqlUtilsDecimalTest.main()");