use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextLString.
/**
* @throws IOException
* @tests java.util.Scanner#next(String)
*/
public void test_nextLString() throws IOException {
s = new Scanner("b*a*").useDelimiter("\\*");
assertEquals("b", s.next("a*b"));
try {
s.next("a*b");
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("word ? ");
assertEquals("word", s.next("\\w+"));
try {
s.next("\\w+");
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("word1 next ");
assertEquals("word1", s.next("\\w+"));
assertEquals("next", s.next("\\w+"));
// test boundary case
try {
s.next("\\w+");
fail();
} catch (NoSuchElementException expected) {
}
// test socket inputStream
os.write("aab 2".getBytes());
serverSocket.close();
s = new Scanner(client);
assertEquals("aab", s.next("a*b"));
try {
s.next("a*b");
fail();
} catch (InputMismatchException expected) {
}
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_hasNextInt.
/**
* @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();
} catch (NoSuchElementException 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();
} catch (InputMismatchException 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٦2");
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٦");
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();
} catch (InputMismatchException expected) {
}
s = new Scanner("03456");
assertTrue(s.hasNextInt());
assertEquals(3456, s.nextInt());
s = new Scanner("٠3,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' '०' '๐'
* 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००");
s.useLocale(Locale.CHINESE);
assertTrue(s.hasNextInt());
assertEquals(12300, s.nextInt());
s = new Scanner("123๐๐");
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();
} catch (InputMismatchException expected) {
}
s = new Scanner("-123 123-");
s.useLocale(new Locale("mk", "MK"));
assertTrue(s.hasNextInt());
assertEquals(-123, s.nextInt());
try {
s.nextInt();
fail();
} catch (InputMismatchException expected) {
}
// Skip the un-recognizable token 123-.
assertEquals("123-", s.next());
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_hasNextLPattern.
/**
* @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();
} catch (InputMismatchException 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();
} catch (InputMismatchException 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();
} catch (NoSuchElementException expected) {
}
s = new Scanner("word1 WorD2 ");
pattern = Pattern.compile("\\w+");
try {
s.hasNext((Pattern) null);
fail();
} catch (NullPointerException expected) {
}
s.close();
try {
s.hasNext(pattern);
fail();
} catch (IllegalStateException 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();
} catch (InputMismatchException expected) {
}
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextShort.
/**
* @throws IOException
* @tests java.util.Scanner#nextShort()
*/
public void test_nextShort() throws IOException {
s = new Scanner("123 456");
assertEquals(123, s.nextShort());
assertEquals(456, s.nextShort());
try {
s.nextShort();
fail();
} catch (NoSuchElementException expected) {
}
// If the radix is different from 10
s = new Scanner("123 456");
s.useRadix(5);
assertEquals(38, s.nextShort());
try {
s.nextShort();
fail();
} catch (InputMismatchException expected) {
}
// If the number is out of range
s = new Scanner("123456789");
try {
s.nextShort();
fail();
} catch (InputMismatchException 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.nextShort();
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(Locale.ENGLISH);
// If exception is thrown out, input will not be advanced.
assertEquals(23456, s.nextShort());
assertEquals(23456, s.nextShort());
/*
* ''' is used in many locales as group separator.
*/
s = new Scanner("23'456 23'456");
s.useLocale(Locale.GERMANY);
try {
s.nextShort();
fail();
} catch (InputMismatchException 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());
/*
* The input string has Arabic-Indic digits.
*/
s = new Scanner("1٠2 1٦2");
assertEquals(102, s.nextShort());
s.useRadix(5);
try {
s.nextShort();
fail();
} catch (InputMismatchException expected) {
}
s.useRadix(10);
assertEquals(162, s.nextShort());
/*
* '.' is used in many locales as group separator. The input string
* has Arabic-Indic digits .
*/
s = new Scanner("23.45٦ 23.456");
s.useLocale(Locale.CHINESE);
try {
s.nextShort();
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(Locale.GERMANY);
// If exception is thrown out, input will not be advanced.
assertEquals(23456, s.nextShort());
assertEquals(23456, s.nextShort());
// The input string starts with zero
s = new Scanner("03,456");
s.useLocale(Locale.ENGLISH);
try {
s.nextShort();
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("03456");
assertEquals(3456, s.nextShort());
s = new Scanner("٠3,456");
s.useLocale(Locale.ENGLISH);
assertEquals(3456, s.nextShort());
s = new Scanner("E34");
s.useRadix(16);
assertEquals(3636, s.nextShort());
/*
* There are 3 types of zero digit in all locales, '0' '०' '๐'
* respectively, but they are not differentiated.
*/
s = new Scanner("12300");
s.useLocale(Locale.CHINESE);
assertEquals(12300, s.nextShort());
s = new Scanner("123००");
s.useLocale(Locale.CHINESE);
assertEquals(12300, s.nextShort());
s = new Scanner("123๐๐");
s.useLocale(Locale.CHINESE);
assertEquals(12300, s.nextShort());
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());
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_hasNextLString.
/**
* @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();
} catch (NullPointerException 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();
} catch (InputMismatchException expected) {
}
s.close();
try {
s.hasNext("a+b");
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("WORD ? ");
assertTrue(s.hasNext("\\w+"));
assertEquals("WORD", s.next("\\w+"));
assertFalse(s.hasNext("\\w+"));
try {
s.next("\\w+");
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("word1 word2 ");
assertEquals("word1", s.next("\\w+"));
assertEquals("word2", s.next("\\w+"));
// test boundary case
try {
s.next("\\w+");
fail();
} catch (NoSuchElementException 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();
} catch (InputMismatchException expected) {
}
}
Aggregations