2013-07-20 21:19:36 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-08-08 04:04:41 +00:00
|
|
|
#include <sstream>
|
2013-09-13 23:14:24 +00:00
|
|
|
#include <string>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
2013-09-26 03:13:05 +00:00
|
|
|
#include "util/name.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/numerics/mpq.h"
|
|
|
|
#include "util/sexpr/sexpr.h"
|
|
|
|
#include "util/sexpr/sexpr_fn.h"
|
|
|
|
#include "util/sexpr/format.h"
|
|
|
|
#include "util/sexpr/options.h"
|
2013-07-20 21:19:36 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
|
|
|
static void tst1() {
|
|
|
|
sexpr s1(30, nil());
|
|
|
|
sexpr s2("name", s1);
|
|
|
|
std::cout << s2 << "\n";
|
|
|
|
std::cout << sexpr(s2, s2) << "\n";
|
|
|
|
lean_assert(len(s2) == 2);
|
|
|
|
lean_assert(is_nil(nil()));
|
|
|
|
lean_assert(!is_nil(s2));
|
|
|
|
lean_assert(is_cons(s2));
|
|
|
|
lean_assert(is_cons(sexpr(s2, s2)));
|
|
|
|
lean_assert(is_list(s2));
|
|
|
|
lean_assert(is_cons(sexpr(s2, s2)));
|
|
|
|
lean_assert(is_list(sexpr(s2, s2)));
|
|
|
|
lean_assert(!is_list(sexpr(s2, sexpr(10))));
|
|
|
|
lean_assert(s2 == sexpr("name", sexpr(30, nil())));
|
|
|
|
lean_assert(s2 != sexpr("name", sexpr(11.2, nil())));
|
|
|
|
lean_assert(s2 != sexpr("name", sexpr(20, nil())));
|
|
|
|
lean_assert(s2 == sexpr("name", sexpr(30, nil())));
|
|
|
|
lean_assert(cdr(s2) == sexpr(30, nil()));
|
|
|
|
lean_assert(car(s2) == sexpr("name"));
|
|
|
|
std::cout << sexpr(name(name("foo"), 1), s2) << "\n";
|
|
|
|
lean_assert(to_mpq(sexpr(mpq("1/3"))) == mpq(1, 3));
|
|
|
|
lean_assert(to_int(sexpr(1)) == 1);
|
|
|
|
lean_assert(is_int(sexpr(1)));
|
|
|
|
lean_assert(!is_nil(sexpr(2)));
|
|
|
|
std::cout << sexpr(sexpr(10), sexpr(20)) << "\n";
|
|
|
|
std::cout << sexpr{10, 20, 30, 40} << "\n";
|
|
|
|
std::cout << sexpr{"foo", "bla", "tst"} << "\n";
|
|
|
|
std::cout << sexpr{10.20, 3.2, 4.3} << "\n";
|
|
|
|
std::cout << sexpr(10.2) << "\n";
|
|
|
|
std::cout << sexpr{10.2} << "\n";
|
|
|
|
lean_assert(!is_list(sexpr(10.2)));
|
|
|
|
lean_assert(is_list(sexpr{10.2}));
|
|
|
|
lean_assert(len(sexpr{10.2}) == 1);
|
|
|
|
// list of pairs
|
2013-09-13 19:49:03 +00:00
|
|
|
std::cout << sexpr{ sexpr(1, 2), sexpr(2, 3), sexpr(0, 1) } << "\n";
|
2013-07-20 21:19:36 +00:00
|
|
|
// list of lists
|
2013-09-13 19:49:03 +00:00
|
|
|
std::cout << sexpr{ sexpr{1, 2}, sexpr{2, 3}, sexpr{0, 1} } << "\n";
|
2013-07-21 01:01:10 +00:00
|
|
|
lean_assert(reverse(sexpr{1, 2, 3}) == (sexpr{3, 2, 1}));
|
|
|
|
sexpr l = map(sexpr{1, 2, 3},
|
|
|
|
[](sexpr e) {
|
|
|
|
return sexpr(to_int(e) + 10);
|
|
|
|
});
|
|
|
|
std::cout << l << std::endl;
|
|
|
|
lean_assert(l == (sexpr{11, 12, 13}));
|
|
|
|
{
|
|
|
|
sexpr l = filter(sexpr{10, -2, 3, -1, 0}, [](sexpr e) { return to_int(e) >= 0; });
|
|
|
|
std::cout << l << std::endl;
|
|
|
|
lean_assert(l == (sexpr{10, 3, 0}));
|
|
|
|
}
|
|
|
|
lean_assert(member(3, sexpr{10, 2, 3, 1}));
|
|
|
|
lean_assert(!member(3, nil()));
|
|
|
|
lean_assert(!member(3, sexpr{10, 2, 1}));
|
2013-09-13 19:49:03 +00:00
|
|
|
lean_assert(append(sexpr{1, 2}, sexpr{3, 4}) == (sexpr{1, 2, 3, 4}));
|
2013-07-21 01:01:10 +00:00
|
|
|
lean_assert(append(l, nil()) == l);
|
|
|
|
lean_assert(append(nil(), l) == l);
|
2013-09-13 19:49:03 +00:00
|
|
|
lean_assert(contains(sexpr{10, 20, -2, 0, 10}, [](sexpr e) { return to_int(e) < 0; }));
|
|
|
|
lean_assert(!contains(sexpr{10, 20, -2, 0, 10}, [](sexpr e) { return to_int(e) < -10; }));
|
2013-08-09 21:02:08 +00:00
|
|
|
lean_assert(is_eqp(s1, s1));
|
2013-07-21 22:59:10 +00:00
|
|
|
sexpr s3 = s1;
|
2013-08-09 21:02:08 +00:00
|
|
|
lean_assert(is_eqp(s1, s3));
|
|
|
|
lean_assert(!is_eqp(sexpr(1), sexpr(1)));
|
2013-07-24 02:27:13 +00:00
|
|
|
lean_assert(is_list(nil()));
|
2013-07-20 21:19:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 21:32:36 +00:00
|
|
|
static void tst2() {
|
2013-07-21 21:25:56 +00:00
|
|
|
sexpr a;
|
|
|
|
a = 2;
|
|
|
|
lean_assert(a == sexpr(2));
|
|
|
|
lean_assert(a == 2);
|
|
|
|
lean_assert(2 == a);
|
|
|
|
a = 0.125;
|
|
|
|
lean_assert(a == sexpr(0.125));
|
|
|
|
lean_assert(a == 0.125);
|
|
|
|
lean_assert(0.125 == a);
|
|
|
|
a = "foo";
|
|
|
|
lean_assert(a == sexpr("foo"));
|
|
|
|
lean_assert(a == "foo");
|
|
|
|
lean_assert("foo" == a);
|
|
|
|
lean_assert(a != "blah");
|
|
|
|
lean_assert(a != name("foo"));
|
|
|
|
lean_assert(std::string("foo") == a);
|
|
|
|
lean_assert(a == std::string("foo"));
|
|
|
|
a = name(name("foo"), 1);
|
|
|
|
lean_assert(a == sexpr(name(name("foo"), 1)));
|
|
|
|
lean_assert(a == name(name("foo"), 1));
|
|
|
|
lean_assert(name(name("foo"), 1) == a);
|
2013-09-13 19:49:03 +00:00
|
|
|
a = mpq(1, 3);
|
|
|
|
lean_assert(a == sexpr(mpq(1, 3)));
|
|
|
|
lean_assert(a == mpq(1, 3));
|
2013-07-21 21:25:56 +00:00
|
|
|
lean_assert(mpq(1, 3) == a);
|
|
|
|
lean_assert(mpq(2, 3) != a);
|
2013-10-22 00:23:56 +00:00
|
|
|
a = pow(mpz(2), 100);
|
|
|
|
lean_assert(a == sexpr(pow(mpz(2), 100)));
|
|
|
|
lean_assert(a == pow(mpz(2), 100));
|
|
|
|
lean_assert(pow(mpz(2), 100) == a);
|
|
|
|
lean_assert(mpq(pow(mpz(2), 100)) != a);
|
2013-07-21 21:25:56 +00:00
|
|
|
lean_assert(sexpr(1, 2) != sexpr(2, 1));
|
|
|
|
lean_assert(sexpr(1, 2) != sexpr(1, sexpr(2, nil())));
|
|
|
|
lean_assert(sexpr(1, 2) == sexpr(1, sexpr(2)));
|
|
|
|
}
|
|
|
|
|
2013-07-21 21:32:36 +00:00
|
|
|
static void tst3() {
|
|
|
|
int sum = 0;
|
2013-08-09 01:35:49 +00:00
|
|
|
for_each(sexpr{0, 1, 2, 3, 4},
|
2013-07-21 21:32:36 +00:00
|
|
|
[&](sexpr const & e) { sum += to_int(e); });
|
|
|
|
lean_assert(sum == 10);
|
|
|
|
}
|
|
|
|
|
2013-07-22 00:05:32 +00:00
|
|
|
static void tst4() {
|
|
|
|
lean_assert(sexpr(1) < sexpr(2));
|
|
|
|
lean_assert(sexpr() < sexpr(2));
|
|
|
|
lean_assert(sexpr("foo") < sexpr(2));
|
|
|
|
lean_assert(sexpr(3) > sexpr(2));
|
|
|
|
lean_assert(sexpr(2) < sexpr(1.0));
|
|
|
|
lean_assert(sexpr("foo") < sexpr(name("foo")));
|
|
|
|
lean_assert(!(sexpr(name("foo")) < sexpr(name("foo"))));
|
|
|
|
auto e = sexpr(1);
|
|
|
|
lean_assert(e == e);
|
|
|
|
lean_assert((sexpr{1, 2, 3}) <= (sexpr{1, 2, 3}));
|
|
|
|
lean_assert((sexpr{1, 2, 3}) >= (sexpr{1, 2, 3}));
|
|
|
|
lean_assert((sexpr{1, 1, 3}) < (sexpr{1, 2, 3}));
|
|
|
|
lean_assert((sexpr{2, 1, 3}) > (sexpr{1, 2, 3}));
|
|
|
|
lean_assert((sexpr{2, 1, 3}) > (sexpr("foo")));
|
|
|
|
lean_assert(sexpr(1, 2) > sexpr(0, 1));
|
|
|
|
}
|
|
|
|
|
2013-07-30 07:50:19 +00:00
|
|
|
static void tst5() {
|
2013-09-13 19:49:03 +00:00
|
|
|
lean_assert(foldl(sexpr{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
2013-07-30 07:50:19 +00:00
|
|
|
0,
|
|
|
|
[](int result, sexpr const & s) {
|
|
|
|
return result * 10 + to_int(s);
|
|
|
|
})
|
|
|
|
== 123456789);
|
|
|
|
|
2013-09-13 19:49:03 +00:00
|
|
|
lean_assert(foldr(sexpr{1, 2, 3, 4, 5, 6, 7, 8, 9},
|
2013-07-30 07:50:19 +00:00
|
|
|
0,
|
|
|
|
[](sexpr const & s, int result) {
|
|
|
|
return result * 10 + to_int(s);
|
|
|
|
})
|
|
|
|
== 987654321);
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:04:41 +00:00
|
|
|
static void tst6() {
|
|
|
|
std::ostringstream s;
|
|
|
|
sexpr foo("str with \"quote\"");
|
|
|
|
s << foo;
|
|
|
|
lean_assert(s.str() == "\"str with \\\"quote\\\"\"");
|
|
|
|
}
|
2013-07-30 07:50:19 +00:00
|
|
|
|
2013-08-09 01:35:49 +00:00
|
|
|
static void tst7() {
|
2013-09-13 19:49:03 +00:00
|
|
|
sexpr s = sexpr{ sexpr(1, 2), sexpr(2, 3), sexpr(0, 1) };
|
2013-08-09 01:35:49 +00:00
|
|
|
std::cout << pp(sexpr{s, s, s, s, s}) << "\n";
|
2013-09-13 19:49:03 +00:00
|
|
|
std::cout << pp(sexpr{sexpr(name{"test", "name"}), sexpr(10), sexpr(10.20)}) << "\n";
|
2013-08-13 23:19:30 +00:00
|
|
|
format f = highlight(pp(sexpr{s, s, s, s, s}));
|
|
|
|
std::cout << f << "\n";
|
|
|
|
std::cout << mk_pair(f, options({"pp", "width"}, 1000)) << "\n";
|
|
|
|
std::cout << mk_pair(f, update(options({"pp", "width"}, 1000), {"pp", "colors"}, false)) << "\n";
|
2013-08-09 01:35:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 15:30:52 +00:00
|
|
|
static void tst8() {
|
|
|
|
lean_assert(!(sexpr("foo") == sexpr(10)));
|
|
|
|
lean_assert(sexpr() == sexpr());
|
|
|
|
lean_assert(sexpr(true) == sexpr(true));
|
|
|
|
lean_assert(sexpr(false) != sexpr(true));
|
|
|
|
sexpr s1(10);
|
|
|
|
sexpr s2 = s1;
|
|
|
|
lean_assert(cmp(s1, s2) == 0);
|
|
|
|
lean_assert(cmp(s1, sexpr(20)) < 0);
|
|
|
|
lean_assert(cmp(sexpr(), sexpr()) == 0);
|
|
|
|
lean_assert(cmp(sexpr("aaa"), sexpr("aaa")) == 0);
|
|
|
|
lean_assert(cmp(sexpr("bbb"), sexpr("aaa")) > 0);
|
|
|
|
lean_assert(cmp(sexpr(true), sexpr(true)) == 0);
|
|
|
|
lean_assert(cmp(sexpr(true), sexpr(false)) > 0);
|
|
|
|
lean_assert(cmp(sexpr(false), sexpr(true)) < 0);
|
|
|
|
lean_assert(cmp(sexpr(1.0), sexpr(2.0)) < 0);
|
|
|
|
lean_assert(cmp(sexpr(name("aaa")), sexpr(name("aaa"))) == 0);
|
|
|
|
lean_assert(cmp(sexpr(name("aaa")), sexpr(name("bbb"))) < 0);
|
|
|
|
lean_assert(cmp(sexpr(mpz(10)), sexpr(mpz(10))) == 0);
|
|
|
|
lean_assert(cmp(sexpr(mpz(20)), sexpr(mpz(10))) > 0);
|
2013-09-13 19:49:03 +00:00
|
|
|
lean_assert(cmp(sexpr(mpq(1, 2)), sexpr(mpq(1, 2))) == 0);
|
|
|
|
lean_assert(cmp(sexpr(mpq(1, 3)), sexpr(mpq(1, 2))) < 0);
|
2013-08-22 15:30:52 +00:00
|
|
|
std::ostringstream s;
|
2013-09-13 19:49:03 +00:00
|
|
|
s << sexpr() << " " << sexpr(mpq(1, 2));
|
2013-08-22 15:30:52 +00:00
|
|
|
std::cout << s.str() << "\n";
|
|
|
|
lean_assert(s.str() == "nil 1/2");
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:46:53 +00:00
|
|
|
static void tst9() {
|
|
|
|
int sz = 100000;
|
|
|
|
sexpr r;
|
|
|
|
for (int i = 0; i < sz; i++) {
|
|
|
|
r = sexpr(sexpr(i), r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 21:19:36 +00:00
|
|
|
int main() {
|
|
|
|
tst1();
|
2013-07-21 21:25:56 +00:00
|
|
|
tst2();
|
2013-07-21 21:32:36 +00:00
|
|
|
tst3();
|
2013-07-22 00:05:32 +00:00
|
|
|
tst4();
|
2013-07-30 07:50:19 +00:00
|
|
|
tst5();
|
2013-08-08 04:04:41 +00:00
|
|
|
tst6();
|
2013-08-09 01:35:49 +00:00
|
|
|
tst7();
|
2013-08-22 15:30:52 +00:00
|
|
|
tst8();
|
2013-11-19 01:46:53 +00:00
|
|
|
tst9();
|
2013-07-21 01:04:05 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
2013-07-20 21:19:36 +00:00
|
|
|
}
|