• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.core;
18 
19 import junit.framework.TestCase;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.StreamTokenizer;
23 import java.io.StringReader;
24 import android.test.suitebuilder.annotation.MediumTest;
25 
26 /**
27  * Tests for the StreamTokenizer
28  */
29 public class StreamTokenizerTest extends TestCase {
30 
31     @MediumTest
testStreamTokenizer()32     public void testStreamTokenizer() throws Exception {
33         String str = "Testing 12345 \n alpha \r\n omega";
34         String strb = "-3.8 'BLIND mice' \r sEe /* how */ they run";
35         StringReader aa = new StringReader(str);
36         StringReader ba = new StringReader(strb);
37         StreamTokenizer a = new StreamTokenizer(aa);
38         StreamTokenizer b = new StreamTokenizer(ba);
39 
40         assertEquals(1, a.lineno());
41         assertEquals(StreamTokenizer.TT_WORD, a.nextToken());
42         assertEquals("Token[Testing], line 1", a.toString());
43         assertEquals(StreamTokenizer.TT_NUMBER, a.nextToken());
44         assertEquals("Token[n=12345.0], line 1", a.toString());
45         assertEquals(StreamTokenizer.TT_WORD, a.nextToken());
46         assertEquals("Token[alpha], line 2", a.toString());
47         assertEquals(StreamTokenizer.TT_WORD, a.nextToken());
48         assertEquals("Token[omega], line 3", a.toString());
49         assertEquals(StreamTokenizer.TT_EOF, a.nextToken());
50         assertEquals("Token[EOF], line 3", a.toString());
51 
52         b.commentChar('u');
53         b.eolIsSignificant(true);
54         b.lowerCaseMode(true);
55         b.ordinaryChar('y');
56         b.slashStarComments(true);
57 
58         assertEquals(StreamTokenizer.TT_NUMBER, b.nextToken());
59         assertEquals(-3.8, b.nval);
60         assertEquals("Token[n=-3.8], line 1", b.toString());
61         assertEquals(39, b.nextToken()); // '
62         assertEquals("Token[BLIND mice], line 1", b.toString());
63         assertEquals(10, b.nextToken()); // \n
64         assertEquals("Token[EOL], line 2", b.toString());
65         assertEquals(StreamTokenizer.TT_WORD, b.nextToken());
66         assertEquals("Token[see], line 2", b.toString());
67         assertEquals(StreamTokenizer.TT_WORD, b.nextToken());
68         assertEquals("Token[the], line 2", b.toString());
69         assertEquals(121, b.nextToken()); // y
70         assertEquals("Token['y'], line 2", b.toString());
71         assertEquals(StreamTokenizer.TT_WORD, b.nextToken());
72         assertEquals("Token[r], line 2", b.toString());
73         assertEquals(StreamTokenizer.TT_EOF, b.nextToken());
74         assertEquals("Token[EOF], line 2", b.toString());
75 
76         // A harmony regression test
77         byte[] data = new byte[]{(byte) '-'};
78         StreamTokenizer tokenizer = new StreamTokenizer(new ByteArrayInputStream(data));
79         tokenizer.nextToken();
80         String result = tokenizer.toString();
81         assertEquals("Token['-'], line 1", result);
82 
83         // another harmony regression test
84         byte[] data2 = new byte[]{(byte) '"',
85                 (byte) 'H',
86                 (byte) 'e',
87                 (byte) 'l',
88                 (byte) 'l',
89                 (byte) 'o',
90                 (byte) '"'};
91         StreamTokenizer tokenizer2 = new StreamTokenizer(new ByteArrayInputStream(data2));
92         tokenizer2.nextToken();
93         result = tokenizer2.toString();
94         assertEquals("Token[Hello], line 1", result);
95     }
96 }
97