Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?rev=422794&r1=422793&r2=422794&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java Mon Jul 17 11:50:18 2006
@@ -1836,552 +1836,518 @@
/**
* @throws IOException
- * @tests java.util.Scanner#hasNext()
+ * @tests java.util.Scanner#nextShort(int)
*/
- public void test_hasNext() throws IOException {
- s = new Scanner("1##2").useDelimiter("\\#");
- assertTrue(s.hasNext());
- assertEquals("1", s.next());
- assertEquals("", s.next());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
- s.close();
- try {
- s.hasNext();
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
- }
-
- s = new Scanner("1( )2( )").useDelimiter("\\( \\)");
- assertTrue(s.hasNext());
- assertTrue(s.hasNext());
- assertEquals("1", s.next());
- assertEquals("2", s.next());
-
- s = new Scanner("1 2 ").useDelimiter("( )");
- assertEquals("1", s.next());
- assertEquals("2", s.next());
- assertTrue(s.hasNext());
- assertEquals("", s.next());
-
- s = new Scanner("1\n2 ");
- assertEquals("1", s.next());
- assertTrue(s.hasNext());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
- // test boundary case
+ public void test_nextShortI() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(123, s.nextShort(10));
+ assertEquals(456, s.nextShort(10));
try {
- s.next();
- fail("should throw NoSuchElementException");
+ s.nextShort(10);
+ fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
- s = new Scanner("1'\n'2 ");
- assertEquals("1'", s.next());
- assertTrue(s.hasNext());
- assertEquals("'2", s.next());
- assertFalse(s.hasNext());
- // test boundary case
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ assertEquals(38, s.nextShort(5));
try {
- s.next();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextShort(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
- s = new Scanner(" ");
- assertFalse(s.hasNext());
-
- // test socket inputStream
-
- os.write("1 2".getBytes());
- serverSocket.close();
-
- s = new Scanner(client);
- assertEquals("1", s.next());
- assertTrue(s.hasNext());
- assertEquals("2", s.next());
- assertFalse(s.hasNext());
+ // If the number is out of range
+ s = new Scanner("123456789");
try {
- s.next();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextShort(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
- }
-
- /**
- * @throws IOException
- * @tests java.util.Scanner#hasNext(Pattern)
- */
- public void test_hasNextLPattern() throws IOException {
- Pattern pattern;
- s = new Scanner("aab@2@abb@").useDelimiter("\\@");
- pattern = Pattern.compile("a*b");
- assertTrue(s.hasNext(pattern));
- assertEquals("aab", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
try {
- s.next(pattern);
- fail("should throw InputMismatchException");
+ s.nextShort(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort(10));
+ assertEquals(23456, s.nextShort(10));
- s = new Scanner("word ? ");
- pattern = Pattern.compile("\\w+");
- assertTrue(s.hasNext(pattern));
- assertEquals("word", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
try {
- s.next(pattern);
- fail("should throw InputMismatchException");
+ s.nextShort(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort(10));
+ assertEquals(23456, s.nextShort(10));
- s = new Scanner("word1 WorD2 ");
- pattern = Pattern.compile("\\w+");
- assertTrue(s.hasNext(pattern));
- assertEquals("word1", s.next(pattern));
- assertTrue(s.hasNext(pattern));
- assertEquals("WorD2", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(102, s.nextShort(10));
try {
- s.next(pattern);
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextShort(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
+ assertEquals(162, s.nextShort(10));
- s = new Scanner("word1 WorD2 ");
- pattern = Pattern.compile("\\w+");
- try {
- s.hasNext((Pattern) null);
- fail("Should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
- }
- s.close();
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
try {
- s.hasNext(pattern);
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
+ s.nextShort(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
}
-
- // test socket inputStream
- os.write("aab b".getBytes());
- serverSocket.close();
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort(10));
+ assertEquals(23456, s.nextShort(10));
- s = new Scanner(client);
- pattern = Pattern.compile("a+b");
- assertTrue(s.hasNext(pattern));
- assertEquals("aab", s.next(pattern));
- assertFalse(s.hasNext(pattern));
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
try {
- s.next(pattern);
- fail("should throw InputMismatchException");
+ s.nextShort(10);
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+
+ s = new Scanner("03456");
+ assertEquals(3456, s.nextShort(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(3456, s.nextShort(10));
+
+ s = new Scanner("E34");
+ assertEquals(3636, s.nextShort(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort(10));
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort(10));
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(-123, s.nextShort(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(-123, s.nextShort(10));
}
-
+
/**
* @throws IOException
- * @tests java.util.Scanner#hasNext(String)
+ * @tests java.util.Scanner#nextShort()
*/
- public void test_hasNextLString() throws IOException {
- s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ public void test_nextShort() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(123, s.nextShort());
+ assertEquals(456, s.nextShort());
try {
- s.hasNext((String)null);
- fail("Should throw NullPointerException");
- } catch (NullPointerException e) {
- // expected
+ s.nextShort();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
}
-
- s = new Scanner("aab*b*").useDelimiter("\\*");
- assertTrue(s.hasNext("a+b"));
- assertEquals("aab", s.next("a+b"));
- assertFalse(s.hasNext("a+b"));
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ s.useRadix(5);
+ assertEquals(38, s.nextShort());
try {
- s.next("a+b");
- fail("should throw InputMismatchException");
+ s.nextShort();
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- s.close();
+
+ // If the number is out of range
+ s = new Scanner("123456789");
try {
- s.hasNext("a+b");
- fail("should throw IllegalStateException");
- } catch (IllegalStateException e) {
- // expected
+ s.nextShort();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
}
- s = new Scanner("WORD ? ");
- assertTrue(s.hasNext("\\w+"));
- assertEquals("WORD", s.next("\\w+"));
- assertFalse(s.hasNext("\\w+"));
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
try {
- s.next("\\w+");
- fail("should throw InputMismatchException");
+ s.nextShort();
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort());
+ assertEquals(23456, s.nextShort());
- s = new Scanner("word1 word2 ");
- assertEquals("word1", s.next("\\w+"));
- assertEquals("word2", s.next("\\w+"));
- // test boundary case
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
try {
- s.next("\\w+");
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
+ s.nextShort();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort());
+ assertEquals(23456, s.nextShort());
- // test socket inputStream
-
- os.write("aab 2".getBytes());
- serverSocket.close();
-
- s = new Scanner(client);
- assertTrue(s.hasNext("a*b"));
- assertEquals("aab", s.next("a*b"));
- assertFalse(s.hasNext("a*b"));
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(102, s.nextShort());
+ s.useRadix(5);
try {
- s.next("a*b");
- fail("should throw InputMismatchException");
+ s.nextShort();
+ fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- }
-
- /**
- * @throws IOException
- * @tests java.util.Scanner#hasNextBoolean()
- */
- public void test_hasNextBoolean() throws IOException {
+ s.useRadix(10);
+ assertEquals(162, s.nextShort());
- s = new Scanner("TRue");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ try {
+ s.nextShort();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextShort());
+ assertEquals(23456, s.nextShort());
- s = new Scanner("tRue false");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
- assertTrue(s.hasNextBoolean());
- assertFalse(s.nextBoolean());
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ try {
+ s.nextShort();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
- s = new Scanner("");
- assertFalse(s.hasNextBoolean());
+ s = new Scanner("03456");
+ assertEquals(3456, s.nextShort());
- // test socket inputStream
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(3456, s.nextShort());
- os.write("true false ".getBytes());
- serverSocket.close();
+ s = new Scanner("E34");
+ s.useRadix(16);
+ assertEquals(3636, s.nextShort());
- s = new Scanner(client);
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort());
- // ues '*' as delimiter
- s = new Scanner("true**false").useDelimiter("\\*");
- assertTrue(s.hasNextBoolean());
- assertTrue(s.nextBoolean());
- assertFalse(s.hasNextBoolean());
- try {
- s.nextBoolean();
- fail("should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
- // Expected
- }
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort());
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertEquals(12300, s.nextShort());
- s = new Scanner("false( )").useDelimiter("\\( \\)");
- assertTrue(s.hasNextBoolean());
- assertFalse(s.nextBoolean());
- assertFalse(s.hasNextBoolean());
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertEquals(-123, s.nextShort());
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertEquals(-123, s.nextShort());
}
/**
* @throws IOException
- * @tests java.util.Scanner#hasNextByte(int)
+ * @tests java.util.Scanner#nextLong(int)
*/
- public void test_hasNextByteI() throws IOException {
- s = new Scanner("123 126");
- assertTrue(s.hasNextByte(10));
- assertEquals(123, s.nextByte(10));
- assertTrue(s.hasNextByte(10));
- assertEquals(126, s.nextByte(10));
- assertFalse(s.hasNextByte(10));
+ public void test_nextLongI() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(123, s.nextLong(10));
+ assertEquals(456, s.nextLong(10));
try {
- s.nextByte(10);
+ s.nextLong(10);
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
// If the radix is different from 10
- s = new Scanner("123 126");
- assertTrue(s.hasNextByte(5));
- assertEquals(38, s.nextByte(5));
- assertFalse(s.hasNextByte(5));
+ s = new Scanner("123 456");
+ assertEquals(38, s.nextLong(5));
try {
- s.nextByte(5);
+ s.nextLong(5);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
// If the number is out of range
- s = new Scanner("1234");
- assertFalse(s.hasNextByte(10));
+ s = new Scanner("123456789123456789123456789123456789");
try {
- s.nextByte(10);
+ s.nextLong(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
+ try {
+ s.nextLong(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextLong(10));
+ assertEquals(23456, s.nextLong(10));
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ try {
+ s.nextLong(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextLong(10));
+ assertEquals(23456, s.nextLong(10));
/*
* The input string has Arabic-Indic digits.
*/
- s = new Scanner("1\u06602 12\u0666");
- assertTrue(s.hasNextByte(10));
- assertEquals(102, s.nextByte(10));
- assertFalse(s.hasNextByte(5));
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(102, s.nextLong(10));
try {
- s.nextByte(5);
+ s.nextLong(5);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- assertTrue(s.hasNextByte(10));
- assertEquals(126, s.nextByte(10));
+ assertEquals(162, s.nextLong(10));
- s = new Scanner("012");
- assertTrue(s.hasNextByte(10));
- assertEquals(12, s.nextByte(10));
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ try {
+ s.nextLong(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextLong(10));
+ assertEquals(23456, s.nextLong(10));
- s = new Scanner("E");
- assertTrue(s.hasNextByte(16));
- assertEquals(14, s.nextByte(16));
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ try {
+ s.nextLong(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("03456");
+ assertEquals(3456, s.nextLong(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(3456, s.nextLong(10));
+
+ s = new Scanner("E34");
+ assertEquals(3636, s.nextLong(16));
/*
* There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
* respectively, but they are not differentiated.
*/
- s = new Scanner("100");
+ s = new Scanner("12300");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte(10));
- assertEquals(100, s.nextByte(10));
+ assertEquals(12300, s.nextLong(10));
- s = new Scanner("1\u0966\u0966");
+ s = new Scanner("123\u0966\u0966");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte(10));
- assertEquals(100, s.nextByte(10));
+ assertEquals(12300, s.nextLong(10));
- s = new Scanner("1\u0e50\u0e50");
+ s = new Scanner("123\u0e50\u0e50");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte(10));
- assertEquals(100, s.nextByte(10));
+ assertEquals(12300, s.nextLong(10));
s = new Scanner("-123");
s.useLocale(new Locale("ar", "AE"));
- assertTrue(s.hasNextByte(10));
- assertEquals(-123, s.nextByte(10));
+ assertEquals(-123, s.nextLong(10));
s = new Scanner("-123");
s.useLocale(new Locale("mk", "MK"));
- assertTrue(s.hasNextByte(10));
- assertEquals(-123, s.nextByte(10));
+ assertEquals(-123, s.nextLong(10));
}
-
+
/**
* @throws IOException
- * @tests java.util.Scanner#hasNextByte()
+ * @tests java.util.Scanner#nextLong()
*/
- public void test_hasNextByte() throws IOException {
- s = new Scanner("123 126");
- assertTrue(s.hasNextByte());
- assertEquals(123, s.nextByte());
- assertTrue(s.hasNextByte());
- assertEquals(126, s.nextByte());
- assertFalse(s.hasNextByte());
+ public void test_nextLong() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(123, s.nextLong());
+ assertEquals(456, s.nextLong());
try {
- s.nextByte();
+ s.nextLong();
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
// If the radix is different from 10
- s = new Scanner("123 126");
+ s = new Scanner("123 456");
s.useRadix(5);
- assertTrue(s.hasNextByte());
- assertEquals(38, s.nextByte());
- assertFalse(s.hasNextByte());
+ assertEquals(38, s.nextLong());
try {
- s.nextByte();
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
// If the number is out of range
- s = new Scanner("1234");
- assertFalse(s.hasNextByte());
+ s = new Scanner("123456789123456789123456789123456789");
try {
- s.nextByte();
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
/*
- * The input string has Arabic-Indic digits.
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
*/
- s = new Scanner("1\u06602 12\u0666");
- assertTrue(s.hasNextByte());
- assertEquals(102, s.nextByte());
- s.useRadix(5);
- assertFalse(s.hasNextByte());
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
try {
- s.nextByte();
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- s.useRadix(10);
- assertTrue(s.hasNextByte());
- assertEquals(126, s.nextByte());
-
- s = new Scanner("012");
- assertEquals(12, s.nextByte());
-
- s = new Scanner("E");
- s.useRadix(16);
- assertTrue(s.hasNextByte());
- assertEquals(14, s.nextByte());
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertEquals(23456, s.nextLong());
+ assertEquals(23456, s.nextLong());
/*
- * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
- * respectively, but they are not differentiated.
- */
- s = new Scanner("100");
- s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte());
- assertEquals(100, s.nextByte());
-
- s = new Scanner("1\u0966\u0966");
- s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte());
- assertEquals(100, s.nextByte());
-
- s = new Scanner("1\u0e50\u0e50");
- s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextByte());
- assertEquals(100, s.nextByte());
-
- s = new Scanner("-123");
- s.useLocale(new Locale("ar", "AE"));
- assertTrue(s.hasNextByte());
- assertEquals(-123, s.nextByte());
-
- s = new Scanner("-123");
- s.useLocale(new Locale("mk", "MK"));
- assertTrue(s.hasNextByte());
- assertEquals(-123, s.nextByte());
- }
-
- /**
- * @throws IOException
- * @tests java.util.Scanner#hasNextBigInteger(int)
- */
- public void test_hasNextBigIntegerI() throws IOException {
- s = new Scanner("123 456");
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("123"), s.nextBigInteger(10));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("456"), s.nextBigInteger(10));
- assertFalse(s.hasNextBigInteger(10));
- try {
- s.nextBigInteger(10);
- fail("Should throw NoSuchElementException");
- } catch (NoSuchElementException e) {
- // Expected
- }
-
- // If the radix is different from 10
- s = new Scanner("123 456");
- assertTrue(s.hasNextBigInteger(5));
- assertEquals(new BigInteger("38"), s.nextBigInteger(5));
- assertFalse(s.hasNextBigInteger(5));
- try {
- s.nextBigInteger(5);
- fail("Should throw InputMismatchException");
- } catch (InputMismatchException e) {
- // Expected
- }
-
- /*
- * Different locale can only recognize corresponding locale sensitive
- * string. ',' is used in many locales as group separator.
- */
- s = new Scanner("23,456 23,456");
- s.useLocale(Locale.GERMANY);
- assertFalse(s.hasNextBigInteger(10));
- try {
- s.nextBigInteger(10);
- fail("Should throw InputMismatchException");
- } catch (InputMismatchException e) {
- // Expected
- }
- s.useLocale(Locale.ENGLISH);
- // If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
-
- /*
- * ''' is used in many locales as group separator.
+ * ''' is used in many locales as group separator.
*/
s = new Scanner("23'456 23'456");
s.useLocale(Locale.GERMANY);
- assertFalse(s.hasNextBigInteger(10));
try {
- s.nextBigInteger(10);
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s.useLocale(new Locale("it", "CH"));
// If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertEquals(23456, s.nextLong());
+ assertEquals(23456, s.nextLong());
/*
* The input string has Arabic-Indic digits.
*/
s = new Scanner("1\u06602 1\u06662");
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("102"), s.nextBigInteger(10));
- assertFalse(s.hasNextBigInteger(5));
+ assertEquals(102, s.nextLong());
+ s.useRadix(5);
try {
- s.nextBigInteger(5);
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("162"), s.nextBigInteger(10));
+ s.useRadix(10);
+ assertEquals(162, s.nextLong());
/*
* '.' is used in many locales as group separator. The input string
@@ -2389,43 +2355,37 @@
*/
s = new Scanner("23.45\u0666 23.456");
s.useLocale(Locale.CHINESE);
- assertFalse(s.hasNextBigInteger(10));
try {
- s.nextBigInteger(10);
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s.useLocale(Locale.GERMANY);
// If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertEquals(23456, s.nextLong());
+ assertEquals(23456, s.nextLong());
// The input string starts with zero
s = new Scanner("03,456");
s.useLocale(Locale.ENGLISH);
- assertFalse(s.hasNextBigInteger(10));
try {
- s.nextBigInteger(10);
+ s.nextLong();
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s = new Scanner("03456");
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+ assertEquals(3456, s.nextLong());
s = new Scanner("\u06603,456");
s.useLocale(Locale.ENGLISH);
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+ assertEquals(3456, s.nextLong());
s = new Scanner("E34");
- assertTrue(s.hasNextBigInteger(16));
- assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
+ s.useRadix(16);
+ assertEquals(3636, s.nextLong());
/*
* There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
@@ -2433,44 +2393,1293 @@
*/
s = new Scanner("12300");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+ assertEquals(12300, s.nextLong());
s = new Scanner("123\u0966\u0966");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+ assertEquals(12300, s.nextLong());
s = new Scanner("123\u0e50\u0e50");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+ assertEquals(12300, s.nextLong());
s = new Scanner("-123");
s.useLocale(new Locale("ar", "AE"));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
-
+ assertEquals(-123, s.nextLong());
s = new Scanner("-123");
s.useLocale(new Locale("mk", "MK"));
- assertTrue(s.hasNextBigInteger(10));
- assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+ assertEquals(-123, s.nextLong());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNext()
+ */
+ public void test_hasNext() throws IOException {
+ s = new Scanner("1##2").useDelimiter("\\#");
+ assertTrue(s.hasNext());
+ assertEquals("1", s.next());
+ assertEquals("", s.next());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ s.close();
+ try {
+ s.hasNext();
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ s = new Scanner("1( )2( )").useDelimiter("\\( \\)");
+ assertTrue(s.hasNext());
+ assertTrue(s.hasNext());
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+
+ s = new Scanner("1 2 ").useDelimiter("( )");
+ assertEquals("1", s.next());
+ assertEquals("2", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("", s.next());
+
+ s = new Scanner("1\n2 ");
+ assertEquals("1", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ // test boundary case
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("1'\n'2 ");
+ assertEquals("1'", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("'2", s.next());
+ assertFalse(s.hasNext());
+ // test boundary case
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner(" ");
+ assertFalse(s.hasNext());
+
+ // test socket inputStream
+
+ os.write("1 2".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertEquals("1", s.next());
+ assertTrue(s.hasNext());
+ assertEquals("2", s.next());
+ assertFalse(s.hasNext());
+ try {
+ s.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNext(Pattern)
+ */
+ public void test_hasNextLPattern() throws IOException {
+ Pattern pattern;
+ s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ pattern = Pattern.compile("a*b");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("aab", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word ? ");
+ pattern = Pattern.compile("\\w+");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("word", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 WorD2 ");
+ pattern = Pattern.compile("\\w+");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("word1", s.next(pattern));
+ assertTrue(s.hasNext(pattern));
+ assertEquals("WorD2", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 WorD2 ");
+ pattern = Pattern.compile("\\w+");
+ try {
+ s.hasNext((Pattern) null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ s.close();
+ try {
+ s.hasNext(pattern);
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ // test socket inputStream
+ os.write("aab b".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ pattern = Pattern.compile("a+b");
+ assertTrue(s.hasNext(pattern));
+ assertEquals("aab", s.next(pattern));
+ assertFalse(s.hasNext(pattern));
+ try {
+ s.next(pattern);
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNext(String)
+ */
+ public void test_hasNextLString() throws IOException {
+ s = new Scanner("aab@2@abb@").useDelimiter("\\@");
+ try {
+ s.hasNext((String)null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ s = new Scanner("aab*b*").useDelimiter("\\*");
+ assertTrue(s.hasNext("a+b"));
+ assertEquals("aab", s.next("a+b"));
+ assertFalse(s.hasNext("a+b"));
+ try {
+ s.next("a+b");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.close();
+ try {
+ s.hasNext("a+b");
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ s = new Scanner("WORD ? ");
+ assertTrue(s.hasNext("\\w+"));
+ assertEquals("WORD", s.next("\\w+"));
+ assertFalse(s.hasNext("\\w+"));
+ try {
+ s.next("\\w+");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("word1 word2 ");
+ assertEquals("word1", s.next("\\w+"));
+ assertEquals("word2", s.next("\\w+"));
+ // test boundary case
+ try {
+ s.next("\\w+");
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // test socket inputStream
+
+ os.write("aab 2".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertTrue(s.hasNext("a*b"));
+ assertEquals("aab", s.next("a*b"));
+ assertFalse(s.hasNext("a*b"));
+ try {
+ s.next("a*b");
+ fail("should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBoolean()
+ */
+ public void test_hasNextBoolean() throws IOException {
+
+ s = new Scanner("TRue");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+
+ s = new Scanner("tRue false");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+ assertTrue(s.hasNextBoolean());
+ assertFalse(s.nextBoolean());
+
+ s = new Scanner("");
+ assertFalse(s.hasNextBoolean());
+
+ // test socket inputStream
+
+ os.write("true false ".getBytes());
+ serverSocket.close();
+
+ s = new Scanner(client);
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+
+ // ues '*' as delimiter
+ s = new Scanner("true**false").useDelimiter("\\*");
+ assertTrue(s.hasNextBoolean());
+ assertTrue(s.nextBoolean());
+ assertFalse(s.hasNextBoolean());
+ try {
+ s.nextBoolean();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("false( )").useDelimiter("\\( \\)");
+ assertTrue(s.hasNextBoolean());
+ assertFalse(s.nextBoolean());
+ assertFalse(s.hasNextBoolean());
+
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextByte(int)
+ */
+ public void test_hasNextByteI() throws IOException {
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(123, s.nextByte(10));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(126, s.nextByte(10));
+ assertFalse(s.hasNextByte(10));
+ try {
+ s.nextByte(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte(5));
+ assertEquals(38, s.nextByte(5));
+ assertFalse(s.hasNextByte(5));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ assertFalse(s.hasNextByte(10));
+ try {
+ s.nextByte(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(102, s.nextByte(10));
+ assertFalse(s.hasNextByte(5));
+ try {
+ s.nextByte(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ assertTrue(s.hasNextByte(10));
+ assertEquals(126, s.nextByte(10));
+
+ s = new Scanner("012");
+ assertTrue(s.hasNextByte(10));
+ assertEquals(12, s.nextByte(10));
+
+ s = new Scanner("E");
+ assertTrue(s.hasNextByte(16));
+ assertEquals(14, s.nextByte(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte(10));
+ assertEquals(100, s.nextByte(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(-123, s.nextByte(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextByte(10));
+ assertEquals(-123, s.nextByte(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextByte()
+ */
+ public void test_hasNextByte() throws IOException {
+ s = new Scanner("123 126");
+ assertTrue(s.hasNextByte());
+ assertEquals(123, s.nextByte());
+ assertTrue(s.hasNextByte());
+ assertEquals(126, s.nextByte());
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 126");
+ s.useRadix(5);
+ assertTrue(s.hasNextByte());
+ assertEquals(38, s.nextByte());
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("1234");
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 12\u0666");
+ assertTrue(s.hasNextByte());
+ assertEquals(102, s.nextByte());
+ s.useRadix(5);
+ assertFalse(s.hasNextByte());
+ try {
+ s.nextByte();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertTrue(s.hasNextByte());
+ assertEquals(126, s.nextByte());
+
+ s = new Scanner("012");
+ assertEquals(12, s.nextByte());
+
+ s = new Scanner("E");
+ s.useRadix(16);
+ assertTrue(s.hasNextByte());
+ assertEquals(14, s.nextByte());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("100");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("1\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextByte());
+ assertEquals(100, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextByte());
+ assertEquals(-123, s.nextByte());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextByte());
+ assertEquals(-123, s.nextByte());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBigInteger(int)
+ */
+ public void test_hasNextBigIntegerI() throws IOException {
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("123"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("456"), s.nextBigInteger(10));
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger(5));
+ assertEquals(new BigInteger("38"), s.nextBigInteger(5));
+ assertFalse(s.hasNextBigInteger(5));
+ try {
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("102"), s.nextBigInteger(10));
+ assertFalse(s.hasNextBigInteger(5));
+ try {
+ s.nextBigInteger(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("162"), s.nextBigInteger(10));
+
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
+
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextBigInteger(10));
+ try {
+ s.nextBigInteger(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("03456");
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
+
+ s = new Scanner("E34");
+ assertTrue(s.hasNextBigInteger(16));
+ assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextBigInteger(10));
+ assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextBigInteger()
+ */
+ public void test_hasNextBigInteger() throws IOException {
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("123"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("456"), s.nextBigInteger());
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ s.useRadix(5);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("38"), s.nextBigInteger());
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456 23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06602 1\u06662");
+ assertEquals(new BigInteger("102"), s.nextBigInteger());
+ s.useRadix(5);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useRadix(10);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("162"), s.nextBigInteger());
+
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666 23.456");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ s.useLocale(Locale.GERMANY);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("23456"), s.nextBigInteger());
+
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextBigInteger());
+ try {
+ s.nextBigInteger();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("03456");
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3456"), s.nextBigInteger());
+
+ s = new Scanner("E34");
+ s.useRadix(16);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("3636"), s.nextBigInteger());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("12300"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
+
+ s = new Scanner("-123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextBigInteger());
+ assertEquals(new BigInteger("-123"), s.nextBigInteger());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextInt(int)
+ */
+ public void test_hasNextIntI() throws IOException {
+ s = new Scanner("123 456");
+ assertEquals(123, s.nextInt(10));
+ assertTrue(s.hasNextInt(10));
+ assertEquals(456, s.nextInt(10));
+ assertFalse(s.hasNextInt(10));
+ try {
+ s.nextInt(10);
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextInt(5));
+ assertEquals(38, s.nextInt(5));
+ assertFalse(s.hasNextInt(5));
+ try {
+ s.nextInt(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("123456789123456789123456789123456789");
+ assertFalse(s.hasNextInt(10));
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextInt(10));
+ s.useLocale(Locale.ENGLISH);
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextInt(10));
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextInt(10));
+ s.useLocale(new Locale("it", "CH"));
+ // If exception is thrown out, input will not be advanced.
+ assertTrue(s.hasNextInt(10));
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06662");
+ assertTrue(s.hasNextInt(10));
+ assertFalse(s.hasNextInt(5));
+
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextInt(10));
+ try {
+ s.nextInt(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextInt(10));
+
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextInt(10));
+ try {
+ s.nextInt(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+
+ s = new Scanner("03456");
+ assertTrue(s.hasNextInt(10));
+ assertEquals(3456, s.nextInt(10));
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextInt(10));
+ assertEquals(3456, s.nextInt(10));
+
+ s = new Scanner("E3456");
+ assertTrue(s.hasNextInt(16));
+ assertEquals(930902, s.nextInt(16));
+ // The following test case fails on RI, because RI does not support
+ // letter as leading digit
+ s = new Scanner("E3,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextInt(16));
+ assertEquals(930902, s.nextInt(16));
+
+ // If parameter radix is illegal, the following test case fails on RI
+ try {
+ s.hasNextInt(Character.MIN_RADIX - 1);
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt(10));
+ assertEquals(12300, s.nextInt(10));
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt(10));
+ assertEquals(12300, s.nextInt(10));
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt(10));
+ assertEquals(12300, s.nextInt(10));
+
+ /*
+ * There are three types of negative prefix all in all. '' '-' '(' There
+ * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
+ * must be used togethor. Prefix '-' and suffix '-' must be used
+ * exclusively.
+ */
+
+ /*
+ * According to Integer regular expression: Integer :: = ( [-+]? (*
+ * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
+ * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
+ * recognized by scanner with locale ar_AE, (123) shouble be recognized
+ * by scanner with locale mk_MK. But this is not the case on RI.
+ */
+ s = new Scanner("-123 123- -123-");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextInt(10));
+ assertEquals(-123, s.nextInt(10));
+ // The following test case fails on RI
+ assertTrue(s.hasNextInt(10));
+ assertEquals(-123, s.nextInt(10));
+ assertFalse(s.hasNextInt(10));
+ try {
+ s.nextInt(10);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+
+ s = new Scanner("-123 123- (123)");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextInt(10));
+ assertEquals(-123, s.nextInt(10));
+ assertFalse(s.hasNextInt(10));
+ try {
+ s.nextInt();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+ // Skip the un-recognizable token 123-.
+ assertEquals("123-", s.next());
+ // The following test case fails on RI
+ assertTrue(s.hasNextInt(10));
+ assertEquals(-123, s.nextInt(10));
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextInt()
+ */
+ public void test_hasNextInt() throws IOException {
+ s = new Scanner("123 456");
+ assertTrue(s.hasNextInt());
+ assertEquals(123, s.nextInt());
+ assertEquals(456, s.nextInt());
+ assertFalse(s.hasNextInt());
+ try {
+ s.nextInt();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ // If the radix is different from 10
+ s = new Scanner("123 456");
+ s.useRadix(5);
+ assertTrue(s.hasNextInt());
+ assertEquals(38, s.nextInt());
+ assertFalse(s.hasNextInt());
+ try {
+ s.nextInt();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("123456789123456789123456789123456789");
+ assertFalse(s.hasNextInt());
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextInt());
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextInt());
+
+ /*
+ * ''' is used in many locales as group separator.
+ */
+ s = new Scanner("23'456");
+ s.useLocale(Locale.GERMANY);
+ assertFalse(s.hasNextInt());
+ s.useLocale(new Locale("it", "CH"));
+ assertTrue(s.hasNextInt());
+
+ /*
+ * The input string has Arabic-Indic digits.
+ */
+ s = new Scanner("1\u06662");
+ s.useRadix(5);
+ assertFalse(s.hasNextInt());
+
+ /*
+ * '.' is used in many locales as group separator. The input string
+ * has Arabic-Indic digits .
+ */
+ s = new Scanner("23.45\u0666");
+ s.useLocale(Locale.CHINESE);
+ assertFalse(s.hasNextInt());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextInt());
+
+ // The input string starts with zero
+ s = new Scanner("03,456");
+ s.useLocale(Locale.ENGLISH);
+ assertFalse(s.hasNextInt());
+ try {
+ s.nextInt();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+
+ s = new Scanner("03456");
+ assertTrue(s.hasNextInt());
+ assertEquals(3456, s.nextInt());
+
+ s = new Scanner("\u06603,456");
+ s.useLocale(Locale.ENGLISH);
+ assertEquals(3456, s.nextInt());
+
+ s = new Scanner("E3456");
+ s.useRadix(16);
+ assertTrue(s.hasNextInt());
+ assertEquals(930902, s.nextInt());
+
+ // The following test case fails on RI, because RI does not support
+ // letter as leading digit
+ s = new Scanner("E3,456");
+ s.useLocale(Locale.ENGLISH);
+ s.useRadix(16);
+ assertTrue(s.hasNextInt());
+ assertEquals(930902, s.nextInt());
+
+ /*
+ * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
+ * respectively, but they are not differentiated.
+ */
+ s = new Scanner("12300");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt());
+ assertEquals(12300, s.nextInt());
+
+ s = new Scanner("123\u0966\u0966");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt());
+ assertEquals(12300, s.nextInt());
+
+ s = new Scanner("123\u0e50\u0e50");
+ s.useLocale(Locale.CHINESE);
+ assertTrue(s.hasNextInt());
+ assertEquals(12300, s.nextInt());
+
+ /*
+ * There are three types of negative prefix all in all. '' '-' '(' There
+ * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
+ * must be used togethor. Prefix '-' and suffix '-' must be used
+ * exclusively.
+ */
+
+ /*
+ * According to Integer regular expression: Integer :: = ( [-+]? (*
+ * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
+ * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
+ * recognized by scanner with locale ar_AE, (123) shouble be recognized
+ * by scanner with locale mk_MK. But this is not the case on RI.
+ */
+ s = new Scanner("-123 123- -123-");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextInt());
+ assertEquals(-123, s.nextInt());
+ // The following test case fails on RI
+ assertTrue(s.hasNextInt());
+ assertEquals(-123, s.nextInt());
+ assertFalse(s.hasNextInt());
+ try {
+ s.nextInt();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+
+ s = new Scanner("-123 123- (123)");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextInt());
+ assertEquals(-123, s.nextInt());
+ try {
+ s.nextInt();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // expected
+ }
+ // Skip the un-recognizable token 123-.
+ assertEquals("123-", s.next());
+ // The following test case fails on RI
+ assertTrue(s.hasNextInt());
+ assertEquals(-123, s.nextInt());
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Scanner#hasNextFloat()
+ */
+ public void test_hasNextFloat() throws IOException {
+ s = new Scanner("123 45\u0666. 123.4 .123 ");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)123.0, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)456.0, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)123.4, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)0.123, s.nextFloat());
+ assertFalse(s.hasNextFloat());
+ try {
+ s.nextFloat();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // Expected
+ }
+
+ s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)123.4, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-456.7, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)123456.789, s.nextFloat());
+ assertFalse(s.hasNextFloat());
+ try {
+ s.nextFloat();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // Scientific notation
+ s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)1.234E12, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-4.567E14, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)1.23456789E-5, s.nextFloat());
+
+ s = new Scanner("NaN Infinity -Infinity");
+ assertTrue(s.hasNextFloat());
+ assertEquals(Float.NaN, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
+ assertTrue(s.hasNextFloat());
+ assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
+
+ String str=String.valueOf(Float.MAX_VALUE*2);
+ s=new Scanner(str);
+ assertTrue(s.hasNextFloat());
+ assertEquals(Float.POSITIVE_INFINITY,s.nextFloat());
+
+ /*
+ * Different locale can only recognize corresponding locale sensitive
+ * string. ',' is used in many locales as group separator.
+ */
+ s = new Scanner("23,456 23,456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23456.0, s.nextFloat());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23.456, s.nextFloat());
+
+ s = new Scanner("23.456 23.456");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23.456, s.nextFloat());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23456.0, s.nextFloat());
+
+ s = new Scanner("23,456.7 23.456,7");
+ s.useLocale(Locale.ENGLISH);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23456.7, s.nextFloat());
+ s.useLocale(Locale.GERMANY);
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)23456.7, s.nextFloat());
+
+ s = new Scanner("-123.4 123.4- -123.4-");
+ s.useLocale(new Locale("ar", "AE"));
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-123.4, s.nextFloat());
+ //The following test case fails on RI
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-123.4, s.nextFloat());
+ try {
+ s.nextFloat();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ s = new Scanner("(123) 123- -123");
+ s.useLocale(new Locale("mk", "MK"));
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-123.0, s.nextFloat());
+ assertFalse(s.hasNextFloat());
+ try {
+ s.nextFloat();
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+ // Skip the un-recognizable token 123-.
+ assertEquals("123-", s.next());
+ assertTrue(s.hasNextFloat());
+ assertEquals((float)-123.0, s.nextFloat());
+
}
/**
* @throws IOException
- * @tests java.util.Scanner#hasNextBigInteger()
+ * @tests java.util.Scanner#hasNextShort(int)
*/
- public void test_hasNextBigInteger() throws IOException {
+ public void test_hasNextShortI() throws IOException {
s = new Scanner("123 456");
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("123"), s.nextBigInteger());
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("456"), s.nextBigInteger());
- assertFalse(s.hasNextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(123, s.nextShort(10));
+ assertTrue(s.hasNextShort(10));
+ assertEquals(456, s.nextShort(10));
+ assertFalse(s.hasNextShort(10));
try {
- s.nextBigInteger();
+ s.nextShort(10);
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
@@ -2478,12 +3687,21 @@
// If the radix is different from 10
s = new Scanner("123 456");
- s.useRadix(5);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("38"), s.nextBigInteger());
- assertFalse(s.hasNextBigInteger());
+ assertTrue(s.hasNextShort(5));
+ assertEquals(38, s.nextShort(5));
+ assertFalse(s.hasNextShort(5));
try {
- s.nextBigInteger();
+ s.nextShort(5);
+ fail("Should throw InputMismatchException");
+ } catch (InputMismatchException e) {
+ // Expected
+ }
+
+ // If the number is out of range
+ s = new Scanner("123456789");
+ assertFalse(s.hasNextShort(10));
+ try {
+ s.nextShort(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
@@ -2495,55 +3713,54 @@
*/
s = new Scanner("23,456 23,456");
s.useLocale(Locale.GERMANY);
- assertFalse(s.hasNextBigInteger());
+ assertFalse(s.hasNextShort(10));
try {
- s.nextBigInteger();
+ s.nextShort(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s.useLocale(Locale.ENGLISH);
// If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextInt(10));
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextInt(10));
/*
* ''' is used in many locales as group separator.
*/
s = new Scanner("23'456 23'456");
s.useLocale(Locale.GERMANY);
- assertFalse(s.hasNextBigInteger());
+ assertFalse(s.hasNextShort(10));
try {
- s.nextBigInteger();
+ s.nextShort(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s.useLocale(new Locale("it", "CH"));
// If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextShort(10));
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextShort(10));
/*
* The input string has Arabic-Indic digits.
*/
s = new Scanner("1\u06602 1\u06662");
- assertEquals(new BigInteger("102"), s.nextBigInteger());
- s.useRadix(5);
- assertFalse(s.hasNextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(102, s.nextShort(10));
+ assertFalse(s.hasNextShort(5));
try {
- s.nextBigInteger();
+ s.nextShort(5);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
- s.useRadix(10);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("162"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(162, s.nextShort(10));
/*
* '.' is used in many locales as group separator. The input string
@@ -2551,44 +3768,43 @@
*/
s = new Scanner("23.45\u0666 23.456");
s.useLocale(Locale.CHINESE);
- assertFalse(s.hasNextBigInteger());
+ assertFalse(s.hasNextShort(10));
try {
- s.nextBigInteger();
+ s.nextShort(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s.useLocale(Locale.GERMANY);
// If exception is thrown out, input will not be advanced.
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("23456"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextShort(10));
+ assertTrue(s.hasNextShort(10));
+ assertEquals(23456, s.nextShort(10));
// The input string starts with zero
s = new Scanner("03,456");
s.useLocale(Locale.ENGLISH);
- assertFalse(s.hasNextBigInteger());
+ assertFalse(s.hasNextShort(10));
try {
- s.nextBigInteger();
+ s.nextShort(10);
fail("Should throw InputMismatchException");
} catch (InputMismatchException e) {
// Expected
}
s = new Scanner("03456");
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("3456"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(3456, s.nextShort(10));
s = new Scanner("\u06603,456");
s.useLocale(Locale.ENGLISH);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("3456"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(3456, s.nextShort(10));
s = new Scanner("E34");
- s.useRadix(16);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("3636"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(16));
+ assertEquals(3636, s.nextShort(16));
/*
* There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
@@ -2596,42 +3812,44 @@
*/
s = new Scanner("12300");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("12300"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(12300, s.nextShort(10));
s = new Scanner("123\u0966\u0966");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("12300"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(12300, s.nextShort(10));
s = new Scanner("123\u0e50\u0e50");
s.useLocale(Locale.CHINESE);
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("12300"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(12300, s.nextShort(10));
s = new Scanner("-123");
s.useLocale(new Locale("ar", "AE"));
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("-123"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(-123, s.nextShort(10));
+
s = new Scanner("-123");
s.useLocale(new Locale("mk", "MK"));
- assertTrue(s.hasNextBigInteger());
- assertEquals(new BigInteger("-123"), s.nextBigInteger());
+ assertTrue(s.hasNextShort(10));
+ assertEquals(-123, s.nextShort(10));
}
-
+
/**
* @throws IOException
- * @tests java.util.Scanner#hasNextInt(int)
+ * @tests java.util.Scanner#hasNextShort()
*/
- public void test_hasNextIntI() throws IOException {
+ public void test_hasNextShort() throws IOException {
s = new Scanner("123 456");
- assertEquals(123, s.nextInt(10));
- assertTrue(s.hasNextInt(10));
- assertEquals(456, s.nextInt(10));
- assertFalse(s.hasNextInt(10));
+ assertTrue(s.hasNextShort());
+ assertEquals(123, s.nextShort());
+ assertTrue(s.hasNextShort());
+ assertEquals(456, s.nextShort());
+ assertFalse(s.hasNextShort());
try {
- s.nextInt(10);
+ s.nextShort();
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
@@ -2639,100 +3857,127 @@
// If the radix is different from 10
s = new Scanner("123 456");
- assertTrue(s.hasNextInt(5));
- assertEquals(38, s.nextInt(5));
- assertFalse(s.hasNextInt(5));
[... 761 lines stripped ...]
|