Program: test
-using GLib;
-
-class Maman.Foo : Object {
- const float[] FLOAT_TESTS = {
- float.EPSILON, 0.0F, 1.0F,
- -float.INFINITY,
- float.INFINITY,
- float.NAN
- };
-
- const double[] DOUBLE_TESTS = {
- double.EPSILON, 0.0, 1.0,
- -double.INFINITY,
- double.INFINITY,
- double.NAN
- };
-
- static void main (string[] args) {
- stdout.printf (
- "float: range=%s...%s\n" +
- " digits=%s(%s), exp=%s..%s(%s..%s)\n" +
- " epsilon=%s, infinity=%s/%s, nan=%s\n",
-
- float.MIN.to_string (),
- float.MAX.to_string (),
-
- float.MANT_DIG.to_string (),
- float.DIG.to_string (),
- float.MIN_EXP.to_string (),
- float.MAX_EXP.to_string (),
- float.MIN_10_EXP.to_string (),
- float.MAX_10_EXP.to_string (),
-
- float.EPSILON.to_string (),
- float.INFINITY.to_string (),
- (-float.INFINITY).to_string (),
- float.NAN.to_string ());
-
- for (int i = 0; i < 6; i++) { // XXX use foreach
- float value = FLOAT_TESTS[i];
- int infinity = value.is_infinity ();
-
- stdout.printf (
- "float(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n",
-
- value,
- value.is_nan () ? "true" : "false",
- value.is_finite () ? "true" : "false",
- value.is_normal () ? "true" : "false",
-
- infinity > 0 ? "positive" :
- infinity < 0 ? "negative" : "none");
- }
-
- stdout.printf (
- "double: range=%s...%s\n" +
- " digits=%s(%s), exp=%s..%s(%s..%s)\n" +
- " epsilon=%s, infinity=%s/%s, nan=%s\n",
-
- double.MIN.to_string (),
- double.MAX.to_string (),
-
- double.MANT_DIG.to_string (),
- double.DIG.to_string (),
- double.MIN_EXP.to_string (),
- double.MAX_EXP.to_string (),
- double.MIN_10_EXP.to_string (),
- double.MAX_10_EXP.to_string (),
-
- double.EPSILON.to_string (),
- double.INFINITY.to_string (),
- (-double.INFINITY).to_string (),
- double.NAN.to_string ());
-
- for (int i = 0; i < 6; i++) { // XXX use foreach
- double value = DOUBLE_TESTS[i];
- int infinity = value.is_infinity ();
-
- stdout.printf(
- "double(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n",
-
- value,
- value.is_nan () ? "true" : "false",
- value.is_finite () ? "true" : "false",
- value.is_normal () ? "true" : "false",
+void test_double () {
+ // declaration and initialization
+ double d = 42d;
+ assert (d == 42d);
+
+ // assignment
+ d = 23d;
+ assert (d == 23d);
+
+ // access
+ double e = d;
+ assert (e == 23d);
+
+ // +
+ d = 42d + 23d;
+ assert (d == 65d);
+
+ // -
+ d = 42d - 23d;
+ assert (d == 19d);
+
+ // *
+ d = 42d * 23d;
+ assert (d == 966d);
+
+ // /
+ d = 42d / 23d;
+ assert (d > 1.8);
+ assert (d < 1.9);
+
+ // equality and relational
+ d = 42d;
+ assert (d == 42d);
+ assert (d != 50d);
+ assert (d < 42.5);
+ assert (!(d < 41.5));
+ assert (d <= 42d);
+ assert (!(d <= 41.5));
+ assert (d >= 42d);
+ assert (!(d >= 42.5));
+ assert (d > 41.5);
+ assert (!(d > 42.5));
+
+ // to_string
+ string s = d.to_string ();
+ assert (s == "42");
+
+ // ensure that MIN and MAX are valid values
+ d = double.MIN;
+ assert (d == double.MIN);
+ assert (d < double.MAX);
+ d = double.MAX;
+ assert (d == double.MAX);
+ assert (d > double.MIN);
+}
- infinity > 0 ? "positive" :
- infinity < 0 ? "negative" : "none");
- }
- }
+void main () {
+ test_double ();
}
/* valascanner.vala
*
- * Copyright (C) 2008 Jürg Billeter
+ * Copyright (C) 2008-2009 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
return TokenType.IDENTIFIER;
}
+ TokenType read_number () {
+ var type = TokenType.INTEGER_LITERAL;
+
+ // integer part
+ if (current < end - 2 && current[0] == '0'
+ && current[1] == 'x' && current[2].isxdigit ()) {
+ // hexadecimal integer literal
+ current += 2;
+ while (current < end && current[0].isxdigit ()) {
+ current++;
+ }
+ } else {
+ // decimal number
+ while (current < end && current[0].isdigit ()) {
+ current++;
+ }
+ }
+
+ // fractional part
+ if (current < end - 1 && current[0] == '.' && current[1].isdigit ()) {
+ type = TokenType.REAL_LITERAL;
+ current++;
+ while (current < end && current[0].isdigit ()) {
+ current++;
+ }
+ }
+
+ // exponent part
+ if (current < end && current[0].tolower () == 'e') {
+ type = TokenType.REAL_LITERAL;
+ current++;
+ if (current < end && (current[0] == '+' || current[0] == '-')) {
+ current++;
+ }
+ while (current < end && current[0].isdigit ()) {
+ current++;
+ }
+ }
+
+ // type suffix
+ if (current < end) {
+ bool real_literal = (type == TokenType.REAL_LITERAL);
+
+ switch (current[0]) {
+ case 'l':
+ case 'L':
+ if (type == TokenType.INTEGER_LITERAL) {
+ current++;
+ if (current < end && current[0].tolower () == 'l') {
+ current++;
+ }
+ }
+ break;
+ case 'u':
+ case 'U':
+ if (type == TokenType.INTEGER_LITERAL) {
+ current++;
+ if (current < end && current[0].tolower () == 'l') {
+ current++;
+ if (current < end && current[0].tolower () == 'l') {
+ current++;
+ }
+ }
+ }
+ break;
+ case 'f':
+ case 'F':
+ case 'd':
+ case 'D':
+ type = TokenType.REAL_LITERAL;
+ current++;
+ break;
+ }
+
+ if (!real_literal && is_ident_char (current[0])) {
+ // allow identifiers to start with a digit
+ // as long as they contain at least one char
+ while (current < end && is_ident_char (current[0])) {
+ current++;
+ }
+ type = TokenType.IDENTIFIER;
+ }
+ }
+
+ return type;
+ }
+
public TokenType read_token (out SourceLocation token_begin, out SourceLocation token_end) {
space ();
}
type = TokenType.IDENTIFIER;
} else if (current[0].isdigit ()) {
- while (current < end && current[0].isdigit ()) {
- current++;
- }
- type = TokenType.INTEGER_LITERAL;
- if (current < end && current[0].tolower () == 'l') {
- current++;
- if (current < end && current[0].tolower () == 'l') {
- current++;
- }
- } else if (current < end && current[0].tolower () == 'u') {
- current++;
- if (current < end && current[0].tolower () == 'l') {
- current++;
- if (current < end && current[0].tolower () == 'l') {
- current++;
- }
- }
- } else if (current < end - 1 && current[0] == '.' && current[1].isdigit ()) {
- current++;
- while (current < end && current[0].isdigit ()) {
- current++;
- }
- if (current < end && current[0].tolower () == 'e') {
- current++;
- if (current < end && (current[0] == '+' || current[0] == '-')) {
- current++;
- }
- while (current < end && current[0].isdigit ()) {
- current++;
- }
- }
- if (current < end && current[0].tolower () == 'f') {
- current++;
- }
- type = TokenType.REAL_LITERAL;
- } else if (current < end && current == begin + 1
- && begin[0] == '0' && begin[1] == 'x' && begin[2].isxdigit ()) {
- // hexadecimal integer literal
- current++;
- while (current < end && current[0].isxdigit ()) {
- current++;
- }
- } else if (current < end && is_ident_char (current[0])) {
- // allow identifiers to start with a digit
- // as long as they contain at least one char
- while (current < end && is_ident_char (current[0])) {
- current++;
- }
- type = TokenType.IDENTIFIER;
- }
+ type = read_number ();
} else {
switch (current[0]) {
case '{':