lix/src/test.cc

110 lines
2.4 KiB
C++
Raw Normal View History

2003-06-15 13:41:32 +00:00
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
2003-06-15 13:41:32 +00:00
#include "hash.hh"
#include "util.hh"
#include "eval.hh"
#include "values.hh"
#include "globals.hh"
2003-06-15 13:41:32 +00:00
void evalTest(Expr e)
2003-06-15 13:41:32 +00:00
{
2003-06-17 13:37:44 +00:00
e = evalValue(e);
cout << (string) hashExpr(e) << ": " << printExpr(e) << endl;
}
2003-06-16 15:59:23 +00:00
struct MySink : DumpSink
{
virtual void operator () (const unsigned char * data, unsigned int len)
{
cout.write((char *) data, len);
}
};
void runTests()
{
/* Hashing. */
string s = "0b0ffd0538622bfe20b92c4aa57254d9";
Hash h = parseHash(s);
if ((string) h != s) abort();
try {
h = parseHash("blah blah");
abort();
} catch (BadRefError err) { };
try {
h = parseHash("0b0ffd0538622bfe20b92c4aa57254d99");
abort();
} catch (BadRefError err) { };
2003-06-16 15:59:23 +00:00
/* Dumping. */
#if 0
MySink sink;
dumpPath("scratch", sink);
cout << (string) hashPath("scratch") << endl;
#endif
/* Set up the test environment. */
mkdir("scratch", 0777);
2003-06-15 13:41:32 +00:00
string testDir = absPath("scratch");
cout << testDir << endl;
nixValues = testDir;
nixLogDir = testDir;
nixDB = testDir + "/db";
initDB();
/* Expression evaluation. */
evalTest(ATmake("Str(\"Hello World\")"));
evalTest(ATmake("Bool(True)"));
evalTest(ATmake("Bool(False)"));
evalTest(ATmake("App(Lam(\"x\", Var(\"x\")), Str(\"Hello World\"))"));
evalTest(ATmake("App(App(Lam(\"x\", Lam(\"y\", Var(\"x\"))), Str(\"Hello World\")), Str(\"Hallo Wereld\"))"));
evalTest(ATmake("App(Lam(\"sys\", Lam(\"x\", [Var(\"x\"), Var(\"sys\")])), Str(\"i686-suse-linux\"))"));
Hash builder1 = addValue("./test-builder-1.sh");
2003-06-17 15:45:43 +00:00
Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",
2003-06-16 21:01:18 +00:00
thisSystem.c_str(), ((string) builder1).c_str());
evalTest(e1);
Hash builder2 = addValue("./test-builder-2.sh");
2003-06-16 21:01:18 +00:00
Expr e2 = ATmake(
2003-06-17 15:45:43 +00:00
"Exec(Str(<str>), Hash(<str>), [Tup(Str(\"src\"), <term>)])",
2003-06-16 21:01:18 +00:00
thisSystem.c_str(), ((string) builder2).c_str(), e1);
evalTest(e2);
2003-06-17 15:45:43 +00:00
2003-06-17 15:47:25 +00:00
Hash h3 = addValue("./test-expr-1.nix");
2003-06-17 15:45:43 +00:00
Expr e3 = ATmake("Deref(Hash(<str>))", ((string) h3).c_str());
evalTest(e3);
}
int main(int argc, char * * argv)
{
ATerm bottomOfStack;
ATinit(argc, argv, &bottomOfStack);
2003-06-15 13:41:32 +00:00
try {
runTests();
} catch (exception & e) {
cerr << "error: " << e.what() << endl;
return 1;
}
2003-06-15 13:41:32 +00:00
}