]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/StringTest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / StringTest.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import java.nio.charset.Charset;
15 import java.util.Random;
16
17 public class StringTest {
18         
19         static final Charset UTF8 = Charset.forName("utf-8");
20         static final Random r = new Random(); 
21         
22         static public String getRandomString()
23         {
24                 int len = r.nextInt(600);
25                 StringBuilder sb = new StringBuilder(len);
26                 for (int i=0; i<len; i++)
27                 {
28                         char c = (char) r.nextInt();
29                         sb.append(c);
30                 }               
31                 return sb.toString();
32         }
33
34         static public String getStringWithC(char c )
35         {
36                 int len = 3;
37                 StringBuilder sb = new StringBuilder(len);
38                 for (int i=0; i<len; i++)
39                 {
40                         sb.append(c);
41                 }               
42                 return sb.toString();
43         }
44         
45         
46         public static int getStringUTF8EncodedByteLength1(String string)
47         {
48                 return string.getBytes(UTF8).length;            
49         }
50
51         public static int getStringUTF8EncodedByteLength2(String string)
52         {
53                 int result = 0;
54                 int length = string.length();
55                 for (int i=0; i<length; i++)
56                 {
57                         char c = string.charAt(i);
58                         if (c>=0 && c<=0x7f) {
59                                 result += 1;
60                         } else if (c>=0x80 && c<=0x07ff) {
61                                 result += 2;
62                         } else if (c>=0xD800 && c<=0xDFFF) {
63                                 result += 1;
64                         } else if (c>=0x800 && c<=0xffff) {
65                                 result += 3;
66                         } else if (c>=0x10000 && c<=0x10ffff) {
67                                 result += 4;
68                         } else if (c>=0x110000 && c<=0x1FFFFF) {
69                                 result += 4;
70                         } else {
71                                 // NOT IN RFC 3629
72                                 // Lets guess 5 bytes anyhow
73                                 result += 4;
74                         }
75                 }
76                 return result;          
77         }
78         
79         
80         public static void main(String[] args) {
81                 char c = (char) 0x66778899;
82                 int j = (int) c;
83                 System.out.println(j );
84                 
85                 for (int i=1000000; i<0x7fffffff; i++)
86                 {
87                         String str = getStringWithC((char)i);
88                         int len1 = getStringUTF8EncodedByteLength1(str);
89                         int len2 = getStringUTF8EncodedByteLength2(str);
90                         if (len1!=len2) {
91                                 System.out.printf("i=%d, Correct=%d, My=%d, Chars=%d, %s\n", i, len1, len2, str.length(), str);
92                                 throw new RuntimeException("length mismatch");
93                         }
94                         if ((i & 0xFFFF) == 0) 
95                                 System.out.printf("i=%d, Correct=%d, My=%d, Chars=%d, %s\n", i, len1, len2, str.length(), str);
96                                 
97                 }
98         }
99         
100 }
101