# 2013 March 10
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the tointeger() and toreal()
# functions.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl

set i 0
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(NULL);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('   ');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('1234');
} {1234}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('   1234');
} {1234}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('bad');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('0xBAD');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('123BAD');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('0x123BAD');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('123NO');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('0x123NO');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('-0x1');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('-0x0');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('0x0');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger('0x1');
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-1);
} {-1}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-0);
} {0}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(0);
} {0}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(1);
} {1}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-1.79769313486232e308 - 1);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-1.79769313486232e308);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-1.79769313486232e308 + 1);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-9223372036854775808 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-9223372036854775808);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-9223372036854775808 + 1);
} {-9223372036854775807}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-2147483648 - 1);
} {-2147483649}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-2147483648);
} {-2147483648}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(-2147483648 + 1);
} {-2147483647}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(2147483647 - 1);
} {2147483646}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(2147483647);
} {2147483647}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(2147483647 + 1);
} {2147483648}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775807 - 1);
} {9223372036854775806}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775807);
} {9223372036854775807}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775807 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(1.79769313486232e308 - 1);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(1.79769313486232e308);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(1.79769313486232e308 + 1);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(4503599627370496 - 1);
} {4503599627370495}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(4503599627370496);
} {4503599627370496}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(4503599627370496 + 1);
} {4503599627370497}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9007199254740992 - 1);
} {9007199254740991}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9007199254740992);
} {9007199254740992}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9007199254740992 + 1);
} {9007199254740993}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775808 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775808);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(9223372036854775808 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(18446744073709551616 - 1);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(18446744073709551616);
} {{}}
do_execsql_test func4-1.[incr i] {
  SELECT tointeger(18446744073709551616 + 1);
} {{}}

ifcapable floatingpoint {
  set i 0
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(NULL);
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('   ');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('1234');
  } {1234.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('   1234');
  } {1234.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('bad');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('0xBAD');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('123BAD');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('0x123BAD');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('123NO');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('0x123NO');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('-0x1');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('-0x0');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('0x0');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal('0x1');
  } {{}}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-1);
  } {-1.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-0);
  } {0.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(0);
  } {0.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(1);
  } {1.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-1.79769313486232e308 - 1);
  } {-Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-1.79769313486232e308);
  } {-Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-1.79769313486232e308 + 1);
  } {-Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-9223372036854775808 - 1);
  } {-9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-9223372036854775808);
  } {-9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-9223372036854775808 + 1);
  } {-9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-2147483648 - 1);
  } {-2147483649.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-2147483648);
  } {-2147483648.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(-2147483648 + 1);
  } {-2147483647.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(2147483647 - 1);
  } {2147483646.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(2147483647);
  } {2147483647.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(2147483647 + 1);
  } {2147483648.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775807 - 1);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775807);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775807 + 1);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(1.79769313486232e308 - 1);
  } {Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(1.79769313486232e308);
  } {Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(1.79769313486232e308 + 1);
  } {Inf}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(4503599627370496 - 1);
  } {4503599627370500.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(4503599627370496);
  } {4503599627370500.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(4503599627370496 + 1);
  } {4503599627370500.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9007199254740992 - 1);
  } {9007199254740990.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9007199254740992);
  } {9007199254740990.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9007199254740992 + 1);
  } {9007199254740990.0}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775808 - 1);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775808);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(9223372036854775808 + 1);
  } {9.22337203685478e+18}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(18446744073709551616 - 1);
  } {1.84467440737096e+19}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(18446744073709551616);
  } {1.84467440737096e+19}
  do_execsql_test func4-2.[incr i] {
    SELECT toreal(18446744073709551616 + 1);
  } {1.84467440737096e+19}
}

ifcapable check {
  set i 0
  do_execsql_test func4-3.[incr i] {
    CREATE TABLE t1(
      x INTEGER CHECK(tointeger(x) IS NOT NULL AND x = CAST(x AS INTEGER))
    );
  } {}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (NULL);
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (NULL);
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('bad');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('1234bad');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('1234.56bad');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (1234);
    }
  } {0 {}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (1234.56);
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('1234');
    }
  } {0 {}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES ('1234.56');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (ZEROBLOB(4));
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (X'');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (X'1234');
    }
  } {1 {constraint failed}}
  do_test func4-3.[incr i] {
    catchsql {
      INSERT INTO t1 (x) VALUES (X'12345678');
    }
  } {1 {constraint failed}}
  do_execsql_test func4-3.[incr i] {
    SELECT x FROM t1 ORDER BY x;
  } {1234 1234}

  ifcapable floatingpoint {
    set i 0
    do_execsql_test func4-4.[incr i] {
      CREATE TABLE t2(
        x REAL CHECK(toreal(x) IS NOT NULL)
      );
    } {}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (NULL);
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (NULL);
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('bad');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('1234bad');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('1234.56bad');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (1234);
      }
    } {0 {}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (1234.56);
      }
    } {0 {}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('1234');
      }
    } {0 {}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES ('1234.56');
      }
    } {0 {}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (ZEROBLOB(4));
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (X'');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (X'1234');
      }
    } {1 {constraint failed}}
    do_test func4-4.[incr i] {
      catchsql {
        INSERT INTO t2 (x) VALUES (X'12345678');
      }
    } {1 {constraint failed}}
    do_execsql_test func4-4.[incr i] {
      SELECT x FROM t2 ORDER BY x;
    } {1234.0 1234.0 1234.56 1234.56}
  }
}

finish_test
