summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/kernel-test.cpp
blob: 44ffe06b7be9c6904dd47be50eb7de0f68ffe315 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

/**
 * Test kernel functions.
 */

#include <assert.h>
#include "string.hpp"
#include "sstream.hpp"
#include "function.hpp"
#include "list.hpp"
#include "tree.hpp"
#include "value.hpp"
#include "monad.hpp"
#include "dynlib.hpp"
#include "perf.hpp"

namespace tuscany {

struct inc {
    int i;
    inc(int i) :
        i(i) {
    }
    const int operator()(const int x) const {
        return x + i;
    }
};

const int square(const int x) {
    return x * x;
}

int mapLambda(lambda<int(const int)> f, int v) {
    return f(v);
}

bool testLambda() {
    const lambda<int(const int)> sq(square);
    assert(sq(2) == 4);
    assert(mapLambda(sq, 2) == 4);
    assert(mapLambda(square, 2) == 4);

    const lambda<int(const int)> incf(inc(10));
    assert(incf(1) == 11);
    assert(mapLambda(incf, 1) == 11);
    assert(mapLambda(inc(10), 1) == 11);

    lambda<int(const int)> l;
    l = incf;
    assert(l(1) == 11);
    l = square;
    assert(l(2) == 4);
    return true;
}

bool testLambdaGC() {
    resetLambdaCounters();
    {
        gc_scoped_pool gc;
        testLambda();
    }
    assert(checkLambdaCounters());
    return true;
}

int countElements = 0;

struct Element {
    int i;

    Element() : i(0) {
        countElements++;
    }

    Element(int i) : i(i) {
        countElements++;
    }

    Element(const Element& o) : i(o.i) {
        countElements++;
    }

    ~Element() {
        countElements--;
    }

    const bool operator==(const Element& o) const {
        return o.i == i;
    }
};
ostream& operator<<(ostream& out, const Element& v) {
    out << v.i ;
    return out;
}

bool testCons() {
    assert(car(cons(2, mklist(3))) == 2);
    assert(car(cdr(cons(2, mklist(3)))) == 3);
    assert(isNil(cdr(cdr(cons(2, mklist(3))))));

    assert(cons(Element(1), mklist(Element(2))) == mklist(Element(1), Element(2)));
    return true;
}

bool testListGC() {
    resetLambdaCounters();
    resetListCounters();
    countElements = 0;
    {
        gc_scoped_pool gc;
        testCons();
    }
    assert(checkLambdaCounters());
    assert(checkListCounters());
    assert(countElements == 0);
    return true;
}

bool testOut() {
    ostringstream os1;
    os1 << list<int> ();
    assert(str(os1) == "()");

    ostringstream os2;
    os2 << mklist(1, 2, 3);
    assert(str(os2) == "(1 2 3)");
    return true;
}

bool testEquals() {
    assert(list<int>() == list<int>());
    assert(mklist(1, 2) == mklist(1, 2));
    assert(list<int>() != mklist(1, 2));
    assert(mklist(1, 2, 3) == mklist(1, 2, 3));
    assert(mklist(1, 2) != mklist(1, 2, 3));
    return true;
}

bool testLength() {
    assert(0 == length(list<int>()));
    assert(1 == length(mklist(1)));
    assert(2 == length(cons(1, mklist(2))));
    return true;
}

bool testAppend() {
    assert(car(append(mklist(1), mklist(2))) == 1);
    assert(car(cdr(append(mklist(1), mklist(2)))) == 2);
    assert(car(cdr(cdr(append(mklist(1), mklist(2, 3))))) == 3);
    assert(isNil(cdr(cdr(cdr(append(mklist(1), mklist(2, 3)))))));

    assert(list<int>() + 1 + 2 + 3 == mklist(1, 2, 3));
    return true;
}

struct Complex {
    int x;
    int y;
    Complex() {
    }
    Complex(int x, int y) :
        x(x), y(y) {
    }
};
ostream& operator<<(ostream& out, const Complex& v) {
    out << "[" << v.x << ":" << v.y << "]";
    return out;
}

bool testComplex() {
    const list<Complex> p = mklist(Complex(1, 2), Complex(3, 4));
    assert(car(p).x == 1);
    assert(car(cdr(p)).x == 3);
    assert(isNil(cdr(cdr(p))));
    return true;
}

bool testMap() {
    assert(isNil(map<int, int>(square, list<int>())));

    const list<int> m = map<int, int>(square, mklist(2, 3));
    assert(car(m) == 4);
    assert(car(cdr(m)) == 9);

    return true;
}

const int add(const int x, const int y) {
    return x + y;
}

bool testReduce() {
    const lambda<int(const int, const int)> r(add);
    assert(reduce(r, 0, mklist(1, 2, 3)) == 6);
    return true;
}

bool isPositive(const int x) {
    if(x >= 0)
        return true;
    else
        return false;
}

bool testFilter() {
    assert(car(filter<int>(isPositive, mklist(1, -1, 2, -2))) == 1);
    assert(cadr(filter<int>(isPositive, mklist(1, -1, 2, -2))) == 2);
    return true;
}

bool testMember() {
    assert(isNil(member(4, mklist(1, 2, 3))));
    assert(car(member(1, mklist(1, 2, 3))) == 1);
    assert(car(member(2, mklist(1, 2, 3))) == 2);
    assert(car(member(3, mklist(1, 2, 3))) == 3);
    return true;
}

bool testReverse() {
    assert(isNil(reverse(list<int>())));
    assert(car(reverse(mklist(1, 2, 3))) == 3);
    assert(cadr(reverse(mklist(1, 2, 3))) == 2);
    return true;
}

bool testListRef() {
    assert(listRef(mklist(1), 0) == 1);
    assert(listRef(mklist(1, 2, 3), 0) == 1);
    assert(listRef(mklist(1, 2, 3), 1) == 2);
    assert(listRef(mklist(1, 2, 3), 2) == 3);
    return true;
}

bool testAssoc() {
    const list<list<string> > l = mklist(mklist<string>("x", "X"), mklist<string>("a", "A"), mklist<string>("y", "Y"), mklist<string>("a", "AA"));
    assert(assoc<string>("a", l) == mklist<string>("a", "A"));
    assert(isNil(assoc<string>("z", l)));

    const list<list<value> > u = mklist(mklist<value>("x", "X"), mklist<value>("a", "A"), mklist<value>("y", "Y"), mklist<value>("a", "AA"));
    assert(assoc<value>("a", u) == mklist<value>("a", "A"));

    const list<value> v = mklist<value>(mklist<value>("x", "X"), mklist<value>("a", "A"), mklist<value>("y", "Y"), mklist<value>("a", "AA"));
    assert(assoc<value>("a", v) == mklist<value>("a", "A"));
    return true;
}

bool testZip() {
    const list<string> k = mklist<string>("x", "a", "y", "a");
    const list<string> v = mklist<string>("X", "A", "Y", "AA");
    const list<list<string> > z = mklist(k, v);
    const list<list<string> > u = mklist(mklist<string>("x", "X"), mklist<string>("a", "A"), mklist<string>("y", "Y"), mklist<string>("a", "AA"));
    assert(zip(k, v) == u);
    assert(unzip(u) == z);
    return true;
}

bool testTokenize() {
    assert(tokenize("/", "aaa/bbb/ccc/ddd") == mklist<string>("aaa", "bbb", "ccc", "ddd"));
    assert(tokenize("/", "/bbb/ccc/ddd") == mklist<string>("", "bbb", "ccc", "ddd"));
    assert(tokenize("/", "/bbb/ccc/") == mklist<string>("", "bbb", "ccc"));
    assert(tokenize("/", "/bbb//ccc/") == mklist<string>("", "bbb", "", "ccc"));
    assert(tokenize("/", "abc/def/") == mklist<string>("abc", "def"));
    return true;
}

double testSeqMap(double x) {
    return x;
}

double testSeqReduce(unused double v, double accum) {
    return accum + 1.0;
}

bool testSeq() {
    resetLambdaCounters();
    resetListCounters();

    list<double> s = seq(0.0, 1000.0);
    assert(1001 == length(s));

    assert(1001 == length(map<double, double>(testSeqMap, s)));

    assert(801 == length(member(200.0, s)));
    assert(201 == length(member(200.0, reverse(s))));

    assert(1001 == (reduce<double, double>(testSeqReduce, 0.0, s)));
    return true;
}

value valueSquare(list<value> x) {
    return (int)car(x) * (int)car(x);
}

bool testValue() {
    assert(value(true) == value(true));
    assert(value(1) == value(1));
    assert(value("abcd") == value("abcd"));
    lambda<value(const list<value>&)> vl(valueSquare);
    assert(value(vl) == value(vl));
    assert(value(mklist<value>(1, 2)) == value(mklist<value>(1, 2)));

    const list<value> v = mklist<value>(mklist<value>("x", "X"), mklist<value>("a", "A"), mklist<value>("y", "Y"));
    assert(cadr((list<list<value> >)value(v)) == mklist<value>("a", "A"));

    const value pv(gc_ptr<value>(new (gc_new<value>()) value(1)));
    assert(*(gc_ptr<value>)pv == value(1));

    const list<value> lpv = mklist<value>(gc_ptr<value>(new (gc_new<value>()) value(1)), gc_ptr<value>(new (gc_new<value>()) value(2)));
    assert(*(gc_ptr<value>)car(lpv) == value(1));
    return true;
}

bool testValueGC() {
    resetLambdaCounters();
    resetListCounters();
    resetValueCounters();
    {
        gc_scoped_pool gc;
        testValue();
    }
    assert(checkValueCounters());
    assert(checkLambdaCounters());
    assert(checkListCounters());
    return true;
}

bool testTree() {
    const list<value> t = mktree<value>("a", list<value>(), list<value>());
    const list<value> ct = constree<value>("d", constree<value>("f", constree<value>("c", constree<value>("e", constree<value>("b", t)))));
    const list<value> mt = mktree(mklist<value>("d", "f", "c", "e", "b", "a"));
    assert(mt == ct);
    const list<value> l = flatten<value>(mt);
    assert(length(l) == 6);
    assert(car(l) == "a");
    assert(car(reverse(l)) == "f");
    const list<value> bt = mkbtree<value>(l);
    assert(car(bt) == "c");
    return true;
}

const list<value> lta(const string& x) {
    return mklist<value>(c_str(x), c_str(x + x));
}

bool testTreeAssoc() {
    const list<value> t = mktree<value>(lta("a"), list<value>(), list<value>());
    const list<value> at = constree<value>(lta("d"), constree<value>(lta("f"), constree<value>(lta("c"), constree<value>(lta("e"), constree<value>(lta("b"), t)))));
    const list<value> l = flatten<value>(at);
    assert(length(l) == 6);
    assert(car(l) == mklist<value>("a", "aa"));
    assert(car(reverse(l)) == mklist<value>("f", "ff"));
    const list<value> bt = mkbtree<value>(l);
    assert(car(bt) == mklist<value>("c", "cc"));
    assert(assoctree<value>("a", bt) == mklist<value>("a", "aa"));
    assert(assoctree<value>("b", bt) == mklist<value>("b", "bb"));
    assert(assoctree<value>("f", bt) == mklist<value>("f", "ff"));
    assert(isNil(assoctree<value>("x", bt)));
    return true;
}

double fib_aux(double n, double a, double b) {
    if(n == 0.0)
        return a;
    return fib_aux(n - 1.0, b, a + b);
}

double fib(double n) {
    return fib_aux(n, 0.0, 1.0);
}

struct fibMapPerf {
    const bool operator()() const {
        list<double> s = seq(0.0, 999.0);
        list<double> r = map<double, double>(fib, s);
        assert(1000 == length(r));
        return true;
    }
};

struct nestedFibMapPerf {
    const lambda<double(const double)> fib;
    nestedFibMapPerf(const lambda<double(const double)>& fib) : fib(fib) {
    }
    const bool operator()() const {
        list<double> s = seq(0.0, 999.0);
        list<double> r = map<double, double>(fib, s);
        assert(1000 == length(r));
        return true;
    }
};

bool testCppPerf() {
    {
        const lambda<bool()> fml = fibMapPerf();
        cout << "Fibonacci map test " << (time(fml, 1, 1) / 1000) << " ms" << endl;
    }

    {
        struct nested {
            static double fib(double n) {
                struct nested {
                    static double fib_aux(double n, double a, double b) {
                        if(n == 0.0)
                            return a;
                        return fib_aux(n - 1.0, b, a + b);
                    }
                };
                return nested::fib_aux(n, 0.0, 1.0);
            }
        };

        const lambda<bool()> nfml = nestedFibMapPerf(lambda<double(const double)>(nested::fib));
        cout << "Nested Fibonacci map test " << (time(nfml, 1, 1) / 1000) << " ms" << endl;
    }
    return true;
}

const id<int> idF(const int v) {
    return v * 2;
}

const id<int> idG(const int v) {
    return v * 3;
}

const id<int> idH(const int v) {
    return idF(v) >> idG;
}

bool testIdMonad() {
    const id<int> m(2);
    assert(m >> idF == idF(2));
    assert(m >> unit<int>() == m);
    assert(m >> idF >> idG == m >> idH);
    return true;
}

const maybe<int> maybeF(const int v) {
    return v * 2;
}

const maybe<int> maybeG(const int v) {
    return v * 3;
}

const maybe<int> maybeH(const int v) {
    return maybeF(v) >> maybeG;
}

bool testMaybeMonad() {
    const maybe<int> m(2);
    assert(m >> maybeF == maybeF(2));
    assert((m >> just<int>()) == m);
    assert(m >> maybeF >> maybeG == m >> maybeH);

    assert(maybe<int>() >> maybeF >> maybeG == maybe<int>());
    return true;
}

const failable<int> failableF(const int v) {
    return v * 2;
}

const failable<int> failableG(const int v) {
    return v * 3;
}

const failable<int> failableH(const int v) {
    return failableF(v) >> failableG;
}

bool testFailableMonad() {
    const failable<int> m(2);
    assert(m >> failableF == failableF(2));
    assert((m >> success<int, string>()) == m);
    assert(m >> failableF >> failableG == m >> failableH);

    cout << "Failable monad test... " << endl;
    failable<int> ooops = mkfailure<int>("test");
    assert(reason(ooops) == "test");
    assert(ooops >> failableF >> failableG == ooops);
    return true;
}

struct tickInc {
    const double v;
    tickInc(const double v) : v(v) {
    }
    const scp<int, double> operator()(int s) const {
        return scp<int, double>(s + 1, v);
    }
};

const state<int, double> tick(const double v) {
    return transformer<int, double>(tickInc(v));
}

const state<int, double> stateF(const double v) {
    return result<int, double>(v * 2.0) >> tick;
}

const state<int, double> stateG(const double v) {
    return result<int, double>(v + 5);
}

const state<int, double> stateH(const double v) {
    return stateF(v) >> stateG;
}

bool testStateMonad() {
    const lambda<state<int, double>(const double)> r(result<int, double>);

    state<int, double> m = result<int, double>(2.0);
    assert((m >> stateF)(0) == stateF(2.0)(0));
    assert(1 == (int)(m >> stateF)(0));
    assert((m >> r)(0) == m(0));
    assert((m >> stateF >> stateG)(0) == (m >> stateH)(0));

    return true;
}

bool testDynLib() {
    const lib dl(string(".libs/libdynlib-test") + dynlibExt);
    const failable<lambda<int(const int)> > sq(dynlambda<int(const int)>("csquare", dl));
    assert(hasContent(sq));
    lambda<int(const int)> l(content(sq));
    assert(l(2) == 4);

    const failable<lambda<lambda<int(const int)>()> > sql(dynlambda<lambda<int(const int)>()>("csquarel", dl));
    assert(hasContent(sql));
    lambda<lambda<int(const int)>()> ll(content(sql));
    assert(ll()(3) == 9);
    return true;
}

}

int main() {
    tuscany::cout << "Testing..." << tuscany::endl;

    tuscany::testLambda();
    tuscany::testLambdaGC();
    tuscany::testCons();
    tuscany::testListGC();
    tuscany::testOut();
    tuscany::testEquals();
    tuscany::testLength();
    tuscany::testAppend();
    tuscany::testComplex();
    tuscany::testMap();
    tuscany::testReduce();
    tuscany::testFilter();
    tuscany::testMember();
    tuscany::testReverse();
    tuscany::testListRef();
    tuscany::testAssoc();
    tuscany::testZip();
    tuscany::testTokenize();
    tuscany::testSeq();
    tuscany::testValue();
    tuscany::testValueGC();
    tuscany::testTree();
    tuscany::testTreeAssoc();
    tuscany::testCppPerf();
    tuscany::testIdMonad();
    tuscany::testMaybeMonad();
    tuscany::testFailableMonad();
    tuscany::testStateMonad();
    tuscany::testDynLib();

    tuscany::cout << "OK" << tuscany::endl;

    return 0;
}