about summary refs log tree commit diff
path: root/src/test.cc
blob: c8e3292e3aee8bae4b32a65ae8727cda883f06ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>

#include <sys/stat.h>
#include <sys/types.h>

#include "hash.hh"
#include "archive.hh"
#include "util.hh"
#include "eval.hh"
#include "values.hh"
#include "globals.hh"


void evalTest(Expr e)
{
    e = evalValue(e);
    cout << (string) hashExpr(e) << ": " << printExpr(e) << endl;
}


struct MySink : DumpSink
{
    virtual void operator () (const unsigned char * data, unsigned int len)
    {
        /* Don't use cout, it's slow as hell! */
        if (write(STDOUT_FILENO, (char *) data, len) != (ssize_t) len)
            throw SysError("writing to stdout");
    }
};


struct MySource : RestoreSource
{
    virtual void operator () (const unsigned char * data, unsigned int len)
    {
        ssize_t res = read(STDIN_FILENO, (char *) data, len);
        if (res == -1)
            throw SysError("reading from stdin");
        if (res != (ssize_t) len)
            throw Error("not enough data available on stdin");
    }
};


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) { };

    /* Dumping. */

#if 0
    MySink sink;
    dumpPath("scratch", sink);
    cout << (string) hashPath("scratch") << endl;
#endif

    /* Restoring. */
#if 1
    MySource source;
    restorePath("outdir", source);
    cout << (string) hashPath("outdir") << endl;
    return;
#endif

    /* Set up the test environment. */

    mkdir("scratch", 0777);

    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");

    Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",
        thisSystem.c_str(), ((string) builder1).c_str());

    evalTest(e1);

    Hash builder2 = addValue("./test-builder-2.sh");

    Expr e2 = ATmake(
        "Exec(Str(<str>), Hash(<str>), [Tup(Str(\"src\"), <term>)])",
        thisSystem.c_str(), ((string) builder2).c_str(), e1);

    evalTest(e2);

    Hash h3 = addValue("./test-expr-1.nix");
    Expr e3 = ATmake("Deref(Hash(<str>))", ((string) h3).c_str());

    evalTest(e3);
}


int main(int argc, char * * argv)
{
    ATerm bottomOfStack;
    ATinit(argc, argv, &bottomOfStack);

    try {
        runTests();
    } catch (exception & e) {
        cerr << "error: " << e.what() << endl;
        return 1;
    }
}