2013-07-21 22:08:14 +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:48:52 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "util/name.h"
|
2013-09-26 21:20:11 +00:00
|
|
|
#include "util/name_generator.h"
|
2013-11-30 18:22:57 +00:00
|
|
|
#include "util/name_set.h"
|
2013-09-13 23:14:24 +00:00
|
|
|
using namespace lean;
|
2013-07-21 22:08:14 +00:00
|
|
|
|
|
|
|
static void tst1() {
|
|
|
|
name n("foo");
|
|
|
|
lean_assert(n == name("foo"));
|
|
|
|
lean_assert(name(n, 1) != name(n, 2));
|
|
|
|
lean_assert(name(n, 1) == name(n, 1));
|
|
|
|
lean_assert(name(name(n, 1), 2) != name(name(n, 1), 1));
|
|
|
|
lean_assert(name(name(n, 1), 1) == name(name(n, 1), 1));
|
|
|
|
lean_assert(name(name(n, 2), 1) != name(name(n, 1), 1));
|
|
|
|
lean_assert(name(name(n, "bla"), 1) == name(name(n, "bla"), 1));
|
|
|
|
lean_assert(name(name(n, "foo"), 1) != name(name(n, "bla"), 1));
|
|
|
|
lean_assert(name(name(name("f"), "bla"), 1) != name(name(n, "bla"), 1));
|
|
|
|
lean_assert(n != name());
|
2013-07-22 11:14:01 +00:00
|
|
|
lean_assert(name().kind() == name_kind::ANONYMOUS);
|
2013-07-21 22:08:14 +00:00
|
|
|
lean_assert(name(name(), "foo") == name("foo"));
|
2013-07-21 22:56:18 +00:00
|
|
|
|
|
|
|
lean_assert(name(n, 1) < name(n, 2));
|
2013-09-13 19:49:03 +00:00
|
|
|
std::cout << "cmp(" << name(n, 1) << ", " << name(n, 2) << ") = " << cmp(name(n, 1), name(n, 2)) << "\n";
|
2013-07-21 22:56:18 +00:00
|
|
|
lean_assert(!(name(n, 1) >= name(n, 2)));
|
|
|
|
lean_assert(name(n, 1) < name(name(n, 1), 1));
|
|
|
|
lean_assert(n < name(n, 1));
|
|
|
|
lean_assert(name(n, 2) > name(name(n, 1), 1));
|
|
|
|
lean_assert(name(name("aa"), 2) < name(name(n, 1), 1));
|
|
|
|
lean_assert(name(n, "aaa") < name(n, "xxx"));
|
|
|
|
lean_assert(name(n, 1) < name(n, "xxx"));
|
|
|
|
lean_assert(name(n, 1) < name(name(n, "xxx"), 1));
|
|
|
|
lean_assert(name() < name(name(n, "xxx"), 1));
|
2013-07-21 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:00:29 +00:00
|
|
|
static name mk_big_name(unsigned num) {
|
|
|
|
name n("foo");
|
|
|
|
for (unsigned i = 0; i < num; i++) {
|
|
|
|
n = name(n, "bla");
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst2() {
|
|
|
|
name n1 = mk_big_name(2000);
|
|
|
|
name n2 = mk_big_name(2000);
|
|
|
|
lean_assert(n1.hash() == n2.hash());
|
|
|
|
for (unsigned i = 0; i < 10000; i++)
|
|
|
|
n1.hash();
|
|
|
|
std::cout << "n1.hash(): " << n1.hash() << "\n";
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:05:11 +00:00
|
|
|
static void tst3() {
|
|
|
|
name n{"foo", "bla", "tst"};
|
|
|
|
std::cout << n << "\n";
|
|
|
|
lean_assert(n == name(name(name("foo"), "bla"), "tst"));
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:43:09 +00:00
|
|
|
static void tst4() {
|
|
|
|
lean_assert(is_prefix_of(name{"foo", "bla"}, name{"foo", "bla"}));
|
|
|
|
lean_assert(is_prefix_of(name{"foo", "bla"}, name{"foo", "bla", "foo"}));
|
|
|
|
lean_assert(is_prefix_of(name{"foo"}, name{"foo", "bla", "foo"}));
|
|
|
|
lean_assert(!is_prefix_of(name{"foo"}, name{"fo", "bla", "foo"}));
|
|
|
|
lean_assert(!is_prefix_of(name{"foo", "bla", "foo"}, name{"foo", "bla"}));
|
|
|
|
lean_assert(is_prefix_of(name{"foo", "bla"}, name(name{"foo", "bla"}, 0u)));
|
|
|
|
}
|
|
|
|
|
2013-08-22 15:48:52 +00:00
|
|
|
static void tst5() {
|
|
|
|
lean_assert(name().size() > 0);
|
|
|
|
lean_assert(name({"foo", "bla", "boing"}).get_prefix() == name({"foo", "bla"}));
|
|
|
|
lean_assert(!name({"foo", "bla", "boing"}).is_atomic());
|
|
|
|
lean_assert(name({"foo"}).is_atomic());
|
|
|
|
lean_assert(strcmp(name({"foo", "bla", "boing"}).get_string(), "boing") == 0);
|
2013-09-24 18:01:30 +00:00
|
|
|
lean_assert(name(name("foo"), 1u).get_numeral() == 1u);
|
2013-08-22 15:48:52 +00:00
|
|
|
lean_assert(name::anonymous().is_anonymous());
|
|
|
|
name n1("foo");
|
|
|
|
name n2 = n1;
|
|
|
|
lean_assert(n2 == n1);
|
|
|
|
std::cout << name::anonymous() << "\n";
|
|
|
|
std::cout << name({"foo", "bla", "boing"}).get_prefix() << "\n";
|
|
|
|
lean_assert(name("foo").is_string());
|
2013-09-24 18:01:30 +00:00
|
|
|
lean_assert(name(name("boo"), "foo").is_string());
|
2013-08-22 15:48:52 +00:00
|
|
|
lean_assert(name(name("foo"), 0u).is_numeral());
|
|
|
|
lean_assert(name(name("foo"), 0u).get_prefix().is_string());
|
|
|
|
}
|
|
|
|
|
2013-09-03 17:09:19 +00:00
|
|
|
static void tst6() {
|
|
|
|
lean_assert(name({"foo", "bla"}).is_safe_ascii());
|
|
|
|
lean_assert(!name({"foo", "b\u2200aaa"}).is_safe_ascii());
|
|
|
|
lean_assert(!name({"\u2200", "boo"}).is_safe_ascii());
|
2013-09-24 18:01:30 +00:00
|
|
|
lean_assert(!name(name(name("baa"), "bla\u2200"), "foo").is_safe_ascii());
|
2013-09-03 17:09:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:07:28 +00:00
|
|
|
static void tst7() {
|
|
|
|
lean_assert(name("foo") + name("bla") == name({"foo", "bla"}));
|
|
|
|
lean_assert(name("foo") + name({"bla", "test"}) == name({"foo", "bla", "test"}));
|
|
|
|
lean_assert(name({"foo", "hello"}) + name({"bla", "test"}) == name({"foo", "hello", "bla", "test"}));
|
2013-09-24 18:01:30 +00:00
|
|
|
lean_assert(name("foo") + (name("bla") + name({"bla", "test"})) == name(name(name(name("foo"), "bla"), "bla"), "test"));
|
2013-09-03 18:07:28 +00:00
|
|
|
lean_assert(name() + name({"bla", "test"}) == name({"bla", "test"}));
|
|
|
|
lean_assert(name({"bla", "test"}) + name() == name({"bla", "test"}));
|
2013-09-24 18:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst8() {
|
|
|
|
name u1 = name::mk_internal_unique_name();
|
|
|
|
name u2 = name::mk_internal_unique_name();
|
|
|
|
lean_assert(u1 != u2);
|
|
|
|
std::cout << u1 << " " << u2 << "\n";
|
2013-09-03 18:07:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:20:11 +00:00
|
|
|
static void tst9() {
|
|
|
|
name_generator g("a");
|
|
|
|
lean_assert(g.prefix() == "a");
|
|
|
|
name n0 = g.next();
|
|
|
|
name n1 = g.next();
|
|
|
|
name a0(name("a"), 0u);
|
|
|
|
name a1(name("a"), 1u);
|
|
|
|
lean_assert(n0 == a0);
|
|
|
|
lean_assert(n1 == a1);
|
|
|
|
}
|
|
|
|
|
2013-11-18 17:13:34 +00:00
|
|
|
static void tst10() {
|
|
|
|
name_generator g1("a");
|
|
|
|
name_generator g2("b");
|
|
|
|
name a0 = g1.next();
|
|
|
|
name b0 = g2.next();
|
|
|
|
lean_assert_eq(a0, name(name("a"), 0u));
|
|
|
|
lean_assert_eq(b0, name(name("b"), 0u));
|
|
|
|
swap(g1, g2);
|
|
|
|
name a1 = g2.next();
|
|
|
|
name b1 = g1.next();
|
|
|
|
lean_assert_eq(a1, name(name("a"), 1u));
|
|
|
|
lean_assert_eq(b1, name(name("b"), 1u));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst11() {
|
|
|
|
name n1("a");
|
|
|
|
name n2("b");
|
|
|
|
swap(n1, n2);
|
|
|
|
lean_assert(n1 == name("b"));
|
|
|
|
lean_assert(n2 == name("a"));
|
|
|
|
}
|
|
|
|
|
2013-11-30 18:22:57 +00:00
|
|
|
static void tst12() {
|
|
|
|
name_set s;
|
|
|
|
s.insert(name("foo"));
|
|
|
|
s.insert(name(name("foo"), 1));
|
|
|
|
s.insert(name("bla"));
|
|
|
|
lean_assert(mk_unique(s, name("boo")) == name("boo"));
|
|
|
|
lean_assert(mk_unique(s, name("bla")) == name(name("bla"), 1));
|
|
|
|
lean_assert(mk_unique(s, name("foo")) == name(name("foo"), 2));
|
|
|
|
}
|
|
|
|
|
2013-07-21 22:08:14 +00:00
|
|
|
int main() {
|
|
|
|
tst1();
|
2013-07-24 18:00:29 +00:00
|
|
|
tst2();
|
2013-08-09 00:05:11 +00:00
|
|
|
tst3();
|
2013-08-19 18:43:09 +00:00
|
|
|
tst4();
|
2013-08-22 15:48:52 +00:00
|
|
|
tst5();
|
2013-09-03 17:09:19 +00:00
|
|
|
tst6();
|
2013-09-03 18:07:28 +00:00
|
|
|
tst7();
|
2013-09-24 18:01:30 +00:00
|
|
|
tst8();
|
2013-09-26 21:20:11 +00:00
|
|
|
tst9();
|
2013-11-18 17:13:34 +00:00
|
|
|
tst10();
|
|
|
|
tst11();
|
2013-11-30 18:22:57 +00:00
|
|
|
tst12();
|
2013-07-21 22:08:14 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|