use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_match.
/**
* @tests java.util.Scanner#match()
*/
public void test_match() {
MatchResult result;
s = new Scanner("1 2 ");
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
assertEquals("1", s.next());
assertEquals("2", s.next());
result = s.match();
assertEquals(2, result.start());
assertEquals(3, result.end());
assertEquals(2, result.start(0));
assertEquals(3, result.end(0));
assertEquals("2", result.group());
assertEquals("2", result.group(0));
assertEquals(0, result.groupCount());
try {
result.start(1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
s.next();
fail();
} catch (NoSuchElementException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True faLse");
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
assertTrue(s.nextBoolean());
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
assertFalse(s.nextBoolean());
try {
s.nextBoolean();
fail();
} catch (NoSuchElementException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True faLse");
assertTrue(s.nextBoolean());
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
s.close();
try {
s.nextBoolean();
fail();
} catch (IllegalStateException expected) {
}
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
s = new Scanner("True fase");
assertTrue(s.nextBoolean());
assertEquals(0, result.groupCount());
try {
s.nextBoolean();
fail();
} catch (InputMismatchException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True fase");
assertTrue(s.nextBoolean());
try {
s.next((Pattern) null);
fail();
} catch (NullPointerException expected) {
}
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextFloat.
/**
* @throws IOException
* @tests java.util.Scanner#nextFloat()
*/
public void test_nextFloat() throws IOException {
s = new Scanner("123 45٦. 123.4 .123 ");
s.useLocale(Locale.ENGLISH);
assertEquals((float) 123.0, s.nextFloat());
assertEquals((float) 456.0, s.nextFloat());
assertEquals((float) 123.4, s.nextFloat());
assertEquals((float) 0.123, s.nextFloat());
try {
s.nextFloat();
fail();
} catch (NoSuchElementException expected) {
}
s = new Scanner("+123.4 -456.7 123,456.789 0.1٢3,4");
s.useLocale(Locale.ENGLISH);
assertEquals((float) 123.4, s.nextFloat());
assertEquals((float) -456.7, s.nextFloat());
assertEquals((float) 123456.789, s.nextFloat());
try {
s.nextFloat();
fail();
} catch (InputMismatchException expected) {
}
// Scientific notation
s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
s.useLocale(Locale.ENGLISH);
assertEquals((float) 1.234E12, s.nextFloat());
assertEquals((float) -4.567E14, s.nextFloat());
assertEquals((float) 1.23456789E-5, s.nextFloat());
s = new Scanner("NaN Infinity -Infinity");
assertEquals(Float.NaN, s.nextFloat());
assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
String str = String.valueOf(Float.MAX_VALUE * 2);
s = new Scanner(str);
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);
assertEquals((float) 23456.0, s.nextFloat());
s.useLocale(Locale.GERMANY);
assertEquals((float) 23.456, s.nextFloat());
s = new Scanner("23.456 23.456");
s.useLocale(Locale.ENGLISH);
assertEquals((float) 23.456, s.nextFloat());
s.useLocale(Locale.GERMANY);
assertEquals((float) 23456.0, s.nextFloat());
s = new Scanner("23,456.7 23.456,7");
s.useLocale(Locale.ENGLISH);
assertEquals((float) 23456.7, s.nextFloat());
s.useLocale(Locale.GERMANY);
assertEquals((float) 23456.7, s.nextFloat());
s = new Scanner("-123.4 123.4- -123.4-");
s.useLocale(new Locale("ar", "AE"));
// assertEquals((float)-123.4, s.nextFloat());
try {
s.nextFloat();
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("123- -123");
s.useLocale(new Locale("mk", "MK"));
try {
s.nextFloat();
fail();
} catch (InputMismatchException expected) {
}
// Skip the un-recognizable token 123-.
assertEquals("123-", s.next());
assertEquals((float) -123.0, s.nextFloat());
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextBigDecimal.
/**
* @throws IOException
* @tests java.util.Scanner#nextBigDecimal()
*/
public void test_nextBigDecimal() throws IOException {
s = new Scanner("123 45٦. 123.4 .123 ");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("123"), s.nextBigDecimal());
assertEquals(new BigDecimal("456"), s.nextBigDecimal());
assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
try {
s.nextBigDecimal();
fail();
} catch (NoSuchElementException expected) {
}
s = new Scanner("+123.4 -456.7 123,456.789 0.1٢3,4");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
try {
s.nextBigDecimal();
fail();
} catch (InputMismatchException expected) {
}
// Scientific notation
s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal());
s = new Scanner("NaN");
try {
s.nextBigDecimal();
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.ENGLISH);
assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
s.useLocale(Locale.GERMANY);
assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
s = new Scanner("23.456 23.456");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
s.useLocale(Locale.GERMANY);
assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
s = new Scanner("23,456.7");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
s = new Scanner("-123.4");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal());
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextBigIntegerI.
/**
* @throws IOException
* @tests java.util.Scanner#nextBigInteger(int)
*/
public void test_nextBigIntegerI() throws IOException {
s = new Scanner("123 456");
assertEquals(new BigInteger("123"), s.nextBigInteger(10));
assertEquals(new BigInteger("456"), s.nextBigInteger(10));
try {
s.nextBigInteger(10);
fail();
} catch (NoSuchElementException expected) {
}
// If the radix is different from 10
s = new Scanner("123 456");
assertEquals(new BigInteger("38"), s.nextBigInteger(5));
try {
s.nextBigInteger(5);
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.nextBigInteger(10);
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(Locale.ENGLISH);
// If exception is thrown out, input will not be advanced.
assertEquals(new BigInteger("23456"), s.nextBigInteger(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);
try {
s.nextBigInteger(10);
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(new Locale("it", "CH"));
// If exception is thrown out, input will not be advanced.
assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
/*
* The input string has Arabic-Indic digits.
*/
s = new Scanner("1٠2 1٦2");
assertEquals(new BigInteger("102"), s.nextBigInteger(10));
try {
s.nextBigInteger(5);
fail();
} catch (InputMismatchException expected) {
}
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٦ 23.456");
s.useLocale(Locale.CHINESE);
try {
s.nextBigInteger(10);
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(Locale.GERMANY);
// If exception is thrown out, input will not be advanced.
assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
// The input string starts with zero
s = new Scanner("03,456");
s.useLocale(Locale.ENGLISH);
try {
s.nextBigInteger(10);
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("03456");
assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
s = new Scanner("٠3,456");
s.useLocale(Locale.ENGLISH);
assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
s = new Scanner("E34");
assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
/*
* 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(new BigInteger("12300"), s.nextBigInteger(10));
s = new Scanner("123००");
s.useLocale(Locale.CHINESE);
assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
s = new Scanner("123๐๐");
s.useLocale(Locale.CHINESE);
assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
s = new Scanner("-123");
s.useLocale(new Locale("ar", "AE"));
assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
s = new Scanner("-123");
s.useLocale(new Locale("mk", "MK"));
assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
}
use of java.util.InputMismatchException in project robovm by robovm.
the class ScannerTest method test_nextShortI.
/**
* @throws IOException
* @tests java.util.Scanner#nextShort(int)
*/
public void test_nextShortI() throws IOException {
s = new Scanner("123 456");
assertEquals(123, s.nextShort(10));
assertEquals(456, s.nextShort(10));
try {
s.nextShort(10);
fail();
} catch (NoSuchElementException expected) {
}
// If the radix is different from 10
s = new Scanner("123 456");
assertEquals(38, s.nextShort(5));
try {
s.nextShort(5);
fail();
} catch (InputMismatchException expected) {
}
// If the number is out of range
s = new Scanner("123456789");
try {
s.nextShort(10);
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(10);
fail();
} catch (InputMismatchException 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));
/*
* ''' is used in many locales as group separator.
*/
s = new Scanner("23'456 23'456");
s.useLocale(Locale.GERMANY);
try {
s.nextShort(10);
fail();
} catch (InputMismatchException 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));
/*
* The input string has Arabic-Indic digits.
*/
s = new Scanner("1٠2 1٦2");
assertEquals(102, s.nextShort(10));
try {
s.nextShort(5);
fail();
} catch (InputMismatchException expected) {
}
assertEquals(162, s.nextShort(10));
/*
* '.' 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(10);
fail();
} catch (InputMismatchException expected) {
}
s.useLocale(Locale.GERMANY);
// If exception is thrown out, input will not be advanced.
assertEquals(23456, s.nextShort(10));
assertEquals(23456, s.nextShort(10));
// The input string starts with zero
s = new Scanner("03,456");
s.useLocale(Locale.ENGLISH);
try {
s.nextShort(10);
fail();
} catch (InputMismatchException expected) {
}
s = new Scanner("03456");
assertEquals(3456, s.nextShort(10));
s = new Scanner("٠3,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' '०' '๐'
* respectively, but they are not differentiated.
*/
s = new Scanner("12300");
s.useLocale(Locale.CHINESE);
assertEquals(12300, s.nextShort(10));
s = new Scanner("123००");
s.useLocale(Locale.CHINESE);
assertEquals(12300, s.nextShort(10));
s = new Scanner("123๐๐");
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));
}
Aggregations