001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.lang;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * TODO: document me.
022: */
023: public class CharacterTest extends GWTTestCase {
024:
025: /**
026: * TODO: document me.
027: */
028: public abstract class Changer {
029: String original;
030:
031: public Changer(String o) {
032: original = o;
033: }
034:
035: public abstract char change(char c);
036:
037: public String changed() {
038: StringBuffer buf = new StringBuffer();
039: for (int i = 0; i < original.length(); i++) {
040: buf.append(change(original.charAt(i)));
041: }
042: return buf.toString();
043: }
044: }
045:
046: /**
047: * TODO: document me.
048: */
049: public abstract class Judge {
050: String original;
051:
052: public Judge(String o) {
053: original = o;
054: }
055:
056: public String allPass() {
057: StringBuffer buf = new StringBuffer();
058: for (int i = 0; i < original.length(); i++) {
059: if (pass(original.charAt(i))) {
060: buf.append(original.charAt(i));
061: }
062: }
063: return buf.toString();
064: }
065:
066: public abstract boolean pass(char c);
067: }
068:
069: class LowerCaseJudge extends Judge {
070: public LowerCaseJudge(String s) {
071: super (s);
072: }
073:
074: public boolean pass(char c) {
075: return Character.isLowerCase(c);
076: }
077: }
078:
079: class UpperCaseJudge extends Judge {
080: public UpperCaseJudge(String s) {
081: super (s);
082: }
083:
084: public boolean pass(char c) {
085: return Character.isUpperCase(c);
086: }
087: }
088:
089: public static String allChars;
090:
091: public static final int NUM_CHARS_HANDLED = 127;
092:
093: static {
094: StringBuffer b = new StringBuffer();
095: for (char c = 0; c < NUM_CHARS_HANDLED; c++) {
096: b.append(c);
097: }
098: allChars = b.toString();
099: }
100:
101: Judge digitJudge = new Judge(allChars) {
102: public boolean pass(char c) {
103: return Character.isDigit(c);
104: }
105: };
106: Judge letterJudge = new Judge(allChars) {
107: public boolean pass(char c) {
108: return Character.isLetter(c);
109: }
110: };
111: Judge letterOrDigitJudge = new Judge(allChars) {
112: public boolean pass(char c) {
113: return Character.isLetterOrDigit(c);
114: }
115: };
116: Changer lowerCaseChanger = new Changer(allChars) {
117: public char change(char c) {
118: return Character.toLowerCase(c);
119: }
120: };
121: Judge lowerCaseJudge = new LowerCaseJudge(allChars);
122: Judge spaceJudge = new Judge(allChars) {
123: public boolean pass(char c) {
124: return Character.isSpace(c);
125: }
126: };
127: Changer upperCaseChanger = new Changer(allChars) {
128: public char change(char c) {
129: return Character.toUpperCase(c);
130: }
131: };
132: Judge upperCaseJudge = new UpperCaseJudge(allChars);
133:
134: public String getModuleName() {
135: return "com.google.gwt.emultest.EmulSuite";
136: }
137:
138: public void testCharValue() {
139: assertEquals(new Character((char) 32).charValue(), (char) 32);
140: }
141:
142: public void testConstructor() {
143: assertEquals(new Character((char) 32), new Character(' '));
144: }
145:
146: public void testDigit() {
147: assertEquals("wrong number of digits", 10, digitJudge.allPass()
148: .length());
149: }
150:
151: public void testLetter() {
152: assertEquals("wrong number of letters", 52, letterJudge
153: .allPass().length());
154: }
155:
156: public void testLetterOrDigit() {
157: assertEquals("wrong number of letters", 62, letterOrDigitJudge
158: .allPass().length());
159: }
160:
161: public void testLowerCase() {
162: assertEquals("wrong number of lowercase letters", 26,
163: lowerCaseJudge.allPass().length());
164: assertEquals(
165: "wrong number of lowercase letters after toLowerCase",
166: 52, new LowerCaseJudge(lowerCaseChanger.changed())
167: .allPass().length());
168: }
169:
170: public void testSpace() {
171: assertEquals("wrong number of spaces", 5, spaceJudge.allPass()
172: .length());
173: }
174:
175: public void testToFromDigit() {
176: for (int i = 0; i < 16; i++) {
177: assertEquals(i, Character.digit(Character.forDigit(i, 16),
178: 16));
179: }
180: assertEquals(1, Character.digit('1', 10));
181: assertEquals('9', Character.forDigit(9, 10));
182: assertEquals(-1, Character.digit('7', 6));
183: assertEquals(-1, Character.digit('8', 8));
184: assertEquals(-1, Character.digit('A', 10));
185: }
186:
187: public void testToString() {
188: assertEquals(new Character((char) 32).toString(), " ");
189: }
190:
191: public void testUpperCase() {
192: assertEquals("wrong number of uppercase letters", 26,
193: upperCaseJudge.allPass().length());
194: assertEquals(
195: "wrong number of uppercase letters after toUpperCase",
196: 52, new UpperCaseJudge(upperCaseChanger.changed())
197: .allPass().length());
198: }
199: }
|