void TestLibrary::booleans() { EnvPtr env = libraryEnvironment(); ValuePtr result = eval(env, "(not #t)"); CHECK("Result is false", result->isBool() && !result->vBool()); result = eval(env, "(not (list 'a 'b 'c))"); CHECK("Result is false", result->isBool() && !result->vBool()); result = eval(env, "(not #f)"); CHECK("Result is true", result->isBool() && result->vBool()); }
bool sEqP(ValuePtr lhs, ValuePtr rhs) { if(lhs->type() != rhs->type()) return false; if(lhs->isBool() && lhs->vBool() == rhs->vBool()) return true; else if(lhs->isNumber() && lhs->isExact() && rhs->isExact() && lhs->vInt() == rhs->vInt()) return true; else if(lhs->isNumber() && !lhs->isExact() && !rhs->isExact() && lhs->vFloat() == rhs->vFloat()) return true; else if(lhs->isString() && string(lhs->vString()) == string(rhs->vString())) return true; else if(lhs->isSymbol() && string(lhs->vString()) == string(rhs->vString())) return true; else if(lhs->isPair() && (lhs == rhs || (lhs->isNull() && rhs->isNull()))) return true; else if(lhs->isProcedure() && (lhs.mValue == rhs.mValue)) return true; else if(lhs->type() == Value::UNDEFINED) return false; return false; }
void TestEval::eval_apply() { EnvPtr env = libraryEnvironment(); ValuePtr result = eval(env, "(list? '(foo bar baz))"); CHECK("Result is boolean", result->isBool() && result->vBool()); }
void TestEval::eval_conditionals() { EnvPtr env = libraryEnvironment(); ValuePtr result = eval(env, "(if #t 'yes 'no)"); CHECK("Symbol type", result->isSymbol()); CHECK_EQUAL("Symbol result", string, "yes", result->vString()); result = eval(env, "(and '(foo) (= 10 10) 1)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(and '(foo) (= 10 20) 1)"); CHECK("Result is false", result->isBool() && !result->vBool()); result = eval(env, "(or (= 10 20) 10 1)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(or #f (= 10 20) (< 10 5))"); CHECK("Result is false", result->isBool() && !result->vBool()); }
void TestEval::eval_simple() { EnvPtr env = new Environment(); env->parent = NULL; ValuePtr value; // bool false value = eval(env, "#f"); CHECK("Boolean false", value->isBool() && !value->vBool()); // integer value = eval(env, "#t"); CHECK("Boolean true", value->isBool() && value->vBool()); // integer value = eval(env, "123"); CHECK("Integer type", value->isNumber() && value->isExact()); CHECK_EQUAL("Integer content", int, 123, value->vInt()); // double value = eval(env, "123.456"); CHECK("Float type", value->isNumber() && !value->isExact()); CHECK_EQUAL("Float content", float, 123.456, value->vFloat()); // string value = eval(env, "\"foobar\""); CHECK("String type", value->isString()); CHECK_EQUAL("string content", string, "foobar", value->vString()); // Symbol ValuePtr intValue = new NumberValue(10); env->values["foo_symbol!"] = intValue; value = eval(env, "foo_symbol!"); CHECK("Symbol lookup", intValue == value); // Quote value = eval(env, "'(foo)"); CHECK("Pair type", value->isPair()); CHECK("Empty list type", value->cdr()->isNull()); CHECK("Symbol type", value->car()->isSymbol()); CHECK_EQUAL("Symbol value", string, "foo", value->car()->vString()); }
void TestLibrary::numerical_operations() { EnvPtr env = libraryEnvironment(); ValuePtr result; // + result = eval(env, "(+ 1 2 3 4 5)"); CHECK("Result is integer", result->isNumber() && result->isExact()); CHECK_EQUAL("Value is correct", int, 15, result->vInt()); // = result = eval(env, "(= 123 123 123)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(= 12.3 12.3 12.3)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(= 123 456 123)"); CHECK("Result is false", result->isBool() && !result->vBool()); // < result = eval(env, "(< 1 2 3 4 5)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(< 1.0 2.0 3.0 4.0 5.0)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(< 6 5 4 3 2)"); CHECK("Result is false", result->isBool() && !result->vBool()); result = eval(env, "(< 10 10 10)"); CHECK("Result is false", result->isBool() && !result->vBool()); // > result = eval(env, "(> 6 5 4 3 2)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(> 5.0 4.0 3.0 2.0 1.0)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(> 1 2 3 4 5)"); CHECK("Result is false", result->isBool() && !result->vBool()); result = eval(env, "(> 10 10 10)"); CHECK("Result is false", result->isBool() && !result->vBool()); // <= result = eval(env, "(<= 1 2 4 4 5)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(<= 1.0 2.0 4.0 4.0 5.0)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(<= 6 5 4 3 2)"); CHECK("Result is false", result->isBool() && !result->vBool()); // >= result = eval(env, "(>= 6 5 4 4 2)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(>= 5.0 4.0 4.0 2.0 1.0)"); CHECK("Result is true", result->isBool() && result->vBool()); result = eval(env, "(>= 1 2 3 4 5)"); CHECK("Result is false", result->isBool() && !result->vBool()); // number->string result = eval(env, "(number->string 10)"); CHECK("Result is string", result->isString()); CHECK_EQUAL("Correct value", string, "10", result->vString()); }