2013-08-08 23:30:35 +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-22 15:08:55 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "util/sexpr/options.h"
|
|
|
|
#include "util/sexpr/option_declarations.h"
|
2013-08-08 23:30:35 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
|
|
|
static void tst1() {
|
|
|
|
options opt;
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
opt = opt.update("tst", 10);
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
opt = opt.update("foo", true);
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
}
|
|
|
|
|
2013-12-28 04:40:00 +00:00
|
|
|
static void check_serializer(options const & o) {
|
|
|
|
std::ostringstream out;
|
|
|
|
serializer s(out);
|
|
|
|
s << o << o;
|
|
|
|
std::istringstream in(out.str());
|
|
|
|
deserializer d(in);
|
|
|
|
options n1, n2;
|
|
|
|
d >> n1 >> n2;
|
|
|
|
lean_assert(o == n1);
|
|
|
|
lean_assert(o == n2);
|
|
|
|
lean_assert(is_eqp(n1, n2));
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:35:49 +00:00
|
|
|
static void tst2() {
|
|
|
|
options opt;
|
|
|
|
opt = update(opt, name{"test", "foo"}, 10);
|
|
|
|
opt = update(opt, name{"color"}, 20);
|
|
|
|
opt = update(opt, name{"ratio"}, 10.5);
|
|
|
|
sexpr s{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
|
|
|
opt = update(opt, name{"sp", "order"}, sexpr{s, s, s, s, s, s});
|
|
|
|
opt = update(opt, name{"test", "long", "names", "with", "several", "parts"}, true);
|
|
|
|
std::cout << pp(opt) << "\n";
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt);
|
2013-08-09 01:35:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 20:55:31 +00:00
|
|
|
static void tst3() {
|
|
|
|
options opt;
|
|
|
|
opt = update(opt, name{"test", "foo"}, 10);
|
|
|
|
opt = update(opt, name{"color"}, 20);
|
|
|
|
std::cout << pp(opt) << "\n";
|
|
|
|
opt = update(opt, name{"color"}, 20);
|
|
|
|
std::cout << pp(opt) << "\n";
|
|
|
|
opt = update(opt, name{"color"}, 30);
|
|
|
|
std::cout << pp(opt) << "\n";
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt);
|
2013-08-20 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 15:08:55 +00:00
|
|
|
static void tst4() {
|
|
|
|
options opt;
|
|
|
|
lean_assert(opt.empty());
|
|
|
|
lean_assert(opt.size() == 0);
|
|
|
|
opt = update(opt, "color", 10);
|
2013-08-22 15:59:39 +00:00
|
|
|
opt = opt.update(name("color"), 10u);
|
2013-08-22 15:08:55 +00:00
|
|
|
lean_assert(!opt.empty());
|
|
|
|
lean_assert(opt.size() == 1);
|
|
|
|
lean_assert(opt.contains("color"));
|
|
|
|
lean_assert(!opt.contains("name"));
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
lean_assert(opt.get_int(name("color"), 0) == 10);
|
|
|
|
lean_assert(opt.get_int("color", 0) == 10);
|
|
|
|
lean_assert(opt.get_int("name", 20) == 20);
|
|
|
|
opt = update(opt, "color", 3);
|
|
|
|
lean_assert(opt.size() == 1);
|
|
|
|
lean_assert(opt.get_int("color", 0) == 3);
|
|
|
|
opt = update(opt, "freq", 0.5);
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
lean_assert(opt.size() == 2);
|
|
|
|
lean_assert(opt.get_double("freq", 0.0) == 0.5);
|
|
|
|
lean_assert(opt.get_double(name("freq"), 0.0) == 0.5);
|
|
|
|
lean_assert(opt.get_unsigned("color", 0) == 3);
|
|
|
|
lean_assert(opt.get_unsigned(name("color"), 0) == 3);
|
|
|
|
opt = update(opt, "name", "Leo");
|
|
|
|
lean_assert(opt.size() == 3);
|
|
|
|
lean_assert(strcmp(opt.get_string("name", ""), "Leo") == 0);
|
2013-08-22 15:51:33 +00:00
|
|
|
lean_assert(strcmp(opt.get_string(name("name"), ""), "Leo") == 0);
|
2013-08-22 15:08:55 +00:00
|
|
|
lean_assert(strcmp(opt.get_string("name2", ""), "Leo") != 0);
|
|
|
|
lean_assert(strcmp(opt.get_string("name2", ""), "") == 0);
|
|
|
|
opt = update(opt, "name", "Soon Ho");
|
|
|
|
lean_assert(opt.size() == 3);
|
|
|
|
lean_assert(strcmp(opt.get_string("name", ""), "Soon Ho") == 0);
|
|
|
|
opt = update(opt, "flag", true);
|
|
|
|
lean_assert(opt.get_bool("flag", false));
|
|
|
|
lean_assert(opt.get_bool(name("flag"), false));
|
|
|
|
lean_assert(!opt.get_bool("flag2", false));
|
|
|
|
opt = update(opt, "list", sexpr{1, 2, 3});
|
|
|
|
std::cout << opt << "\n";
|
|
|
|
lean_assert(opt.get_sexpr("list", sexpr()) == sexpr({1, 2, 3}));
|
|
|
|
lean_assert(opt.get_sexpr(name("list"), sexpr()) == sexpr({1, 2, 3}));
|
|
|
|
lean_assert(opt.contains("name"));
|
|
|
|
lean_assert(!opt.contains("name2"));
|
|
|
|
lean_assert(opt.contains("color"));
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt);
|
2013-08-22 15:08:55 +00:00
|
|
|
options opt2;
|
|
|
|
opt2 = update(opt2, "name", "Leo");
|
|
|
|
opt2 = update(opt2, "value", 10);
|
|
|
|
opt2 = update(opt2, "flag", false);
|
|
|
|
std::cout << "# " << opt << "\n# " << opt2 << "\n";
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt2);
|
2013-08-22 15:08:55 +00:00
|
|
|
options opt3 = join(opt, opt2);
|
|
|
|
std::cout << "> " << opt3 << "\n";
|
|
|
|
lean_assert(strcmp(opt3.get_string("name", ""), "Leo") == 0);
|
|
|
|
lean_assert(opt3.get_unsigned("value", 0) == 10);
|
|
|
|
lean_assert(opt3.get_unsigned("color", 0) == 3);
|
|
|
|
lean_assert(opt3.get_double(name("freq"), 0.0) == 0.5);
|
|
|
|
lean_assert(opt3.get_unsigned(name("freq"), 0) == 0);
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt3);
|
2013-08-22 15:08:55 +00:00
|
|
|
std::ostringstream s;
|
|
|
|
option_kind k;
|
|
|
|
k = BoolOption; s << k << " ";
|
|
|
|
k = IntOption; s << k << " ";
|
|
|
|
k = UnsignedOption; s << k << " ";
|
|
|
|
k = DoubleOption; s << k << " ";
|
|
|
|
k = StringOption; s << k << " ";
|
|
|
|
k = SExprOption; s << k;
|
|
|
|
std::cout << s.str() << "\n";
|
|
|
|
lean_assert(s.str() == "Bool Int Unsigned Int Double String S-Expression");
|
|
|
|
}
|
|
|
|
|
2013-08-22 15:59:39 +00:00
|
|
|
RegisterBoolOption("fakeopt", false, "fake option");
|
|
|
|
|
|
|
|
static void tst5() {
|
|
|
|
option_declarations const & decls = get_option_declarations();
|
|
|
|
auto it = decls.find("fakeopt");
|
|
|
|
lean_assert(it != decls.end());
|
|
|
|
auto decl = it->second;
|
|
|
|
lean_assert(decl.get_name() == "fakeopt");
|
|
|
|
lean_assert(decl.get_default_value() == "false");
|
|
|
|
lean_assert(decl.get_description() == "fake option");
|
|
|
|
auto it2 = decls.find("fakeopt2");
|
|
|
|
lean_assert(it2 == decls.end());
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:53:53 +00:00
|
|
|
static void tst6() {
|
|
|
|
options opt, opt2;
|
|
|
|
lean_assert(is_eqp(opt, opt2));
|
|
|
|
opt = update(opt, name{"test", "foo"}, 10);
|
|
|
|
lean_assert(!is_eqp(opt, opt2));
|
|
|
|
opt2 = update(opt2, name{"test", "foo"}, 10);
|
|
|
|
lean_assert(!is_eqp(opt, opt2));
|
|
|
|
opt2 = opt;
|
|
|
|
lean_assert(is_eqp(opt, opt2));
|
2013-12-28 04:40:00 +00:00
|
|
|
check_serializer(opt);
|
2013-12-20 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:30:35 +00:00
|
|
|
int main() {
|
2013-12-07 22:59:21 +00:00
|
|
|
save_stack_info();
|
2013-08-08 23:30:35 +00:00
|
|
|
tst1();
|
2013-08-09 01:35:49 +00:00
|
|
|
tst2();
|
2013-08-20 20:55:31 +00:00
|
|
|
tst3();
|
2013-08-22 15:08:55 +00:00
|
|
|
tst4();
|
2013-08-22 15:59:39 +00:00
|
|
|
tst5();
|
2013-12-20 18:53:53 +00:00
|
|
|
tst6();
|
2013-08-08 23:30:35 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|