~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/src/ndbjtie/test/test/MySqlUtilsDecimalTest.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright 2010 Sun Microsystems, Inc.
 
3
 All rights reserved. Use is subject to license terms.
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
*/
 
18
/*
 
19
 * MySqlUtilsDecimalTest.java
 
20
 */
 
21
 
 
22
package test;
 
23
 
 
24
import java.io.PrintWriter;
 
25
import java.nio.ByteBuffer;
 
26
import java.nio.charset.Charset;
 
27
 
 
28
import com.mysql.ndbjtie.mysql.Utils;
 
29
 
 
30
/**
 
31
 * Tests the basic functioning of the NdbJTie libary: mysql decimal utils
 
32
 * for string-binary conversions.
 
33
 */
 
34
public class MySqlUtilsDecimalTest extends JTieTestBase {
 
35
 
 
36
    public void test_s2b2s(String s, int prec, int scale) {
 
37
        out.print("    [" + prec + "/" + scale + "] '" + s + "' => ");
 
38
        out.flush();
 
39
 
 
40
        // write string buffer
 
41
        final ByteBuffer sbuf = ByteBuffer.allocateDirect(128);
 
42
        final byte[] b;
 
43
        try {
 
44
            b = s.getBytes("US-ASCII");
 
45
        } catch (java.io.UnsupportedEncodingException ex) {
 
46
            throw new RuntimeException(ex);
 
47
        }
 
48
        assert (s.equals(new String(b)));
 
49
        assert (b.length < sbuf.capacity());
 
50
        sbuf.put(b);
 
51
        assert (b.length == sbuf.position());
 
52
        sbuf.flip();
 
53
        assert (b.length == sbuf.limit());
 
54
 
 
55
        // clear binary buffer
 
56
        final ByteBuffer bbuf = ByteBuffer.allocateDirect(128);
 
57
        bbuf.clear();
 
58
        for (int i = 0; i < bbuf.capacity(); i++) bbuf.put((byte)0);
 
59
        bbuf.rewind();
 
60
 
 
61
        // string->binary
 
62
        assert (sbuf.position() == 0);
 
63
        assert (bbuf.position() == 0);
 
64
        final int r1 = Utils.decimal_str2bin(sbuf, sbuf.capacity(),
 
65
                                             prec, scale,
 
66
                                             bbuf, bbuf.capacity());
 
67
        if (r1 != Utils.E_DEC_OK) {
 
68
            out.println("decimal_str2bin() returned: " + r1);
 
69
            return;
 
70
        }
 
71
 
 
72
        // clear string buffer
 
73
        sbuf.clear();
 
74
        for (int i = 0; i < sbuf.capacity(); i++) sbuf.put((byte)0);
 
75
        sbuf.rewind();
 
76
        
 
77
        // binary->string
 
78
        assert (bbuf.position() == 0);
 
79
        assert (sbuf.position() == 0);
 
80
        final int r2 = Utils.decimal_bin2str(bbuf, bbuf.capacity(),
 
81
                                             prec, scale,
 
82
                                             sbuf, sbuf.capacity());
 
83
        if (r2 != Utils.E_DEC_OK) {
 
84
            out.println("decimal_bin2str() returned: " + r2);
 
85
            return;
 
86
        }
 
87
 
 
88
        // read string buffer
 
89
        assert (sbuf.position() == 0);
 
90
        sbuf.limit(prec);
 
91
        final String t = Charset.forName("US-ASCII").decode(sbuf).toString();
 
92
 
 
93
        out.println("'" + t + "'");
 
94
    }
 
95
 
 
96
    public void test() {
 
97
        out.println("--> MySqlUtilsDecimalTest.test()");
 
98
 
 
99
        // load native library
 
100
        loadSystemLibrary("ndbclient");
 
101
 
 
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);
 
110
 
 
111
        out.println();
 
112
        out.println("<-- MySqlUtilsDecimalTest.test()");
 
113
    };
 
114
    
 
115
    static public void main(String[] args) throws Exception {
 
116
        out.println("--> MySqlUtilsDecimalTest.main()");
 
117
 
 
118
        out.println();
 
119
        MySqlUtilsDecimalTest test = new MySqlUtilsDecimalTest();
 
120
        test.test();
 
121
        
 
122
        out.println();
 
123
        out.println("<-- MySqlUtilsDecimalTest.main()");
 
124
    }
 
125
}