OSDN Git Service

bignumの再実装中
[simplecms/utakata.git] / src / test / environment_test.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <functional>
5
6 #include "../simpletest.h"
7
8 #include "../environment.h"
9 #include "../base_object.h"
10
11 #include "../data_structure_interface.h"
12 #include "../data_structure.h"
13 #include "../object.h"
14 #include "../type.h"
15
16 #include "../unicode.h"
17
18
19 using namespace std;
20 using namespace utakata;
21
22 class String : public data::DataEntity
23 {
24 public:
25     String(std::string str) : str_(new unicode::UniString(unicode::convert(str)))
26         {}
27     virtual ~String() {}
28
29     virtual void* getAddress() {return reinterpret_cast<unsigned char*>(&(*str_));}
30     virtual const void* getAddress() const {return reinterpret_cast<unsigned char*>(&(*str_));}
31
32     smart_ptr<unicode::UniString> str_;
33 };
34
35 bool environment_test(smart_ptr<simpletest::SimpleTestAsserter> asserter)
36 {
37     // Schemeの「環境」として扱われるクラスのテスト。
38     // data::Environmentは、ある時点での環境を表す。
39     // 環境とは、ある時点での可視である束縛変数(キーワード含む)の集合
40     // であり、定義された時点で可視である変数を全て含む。
41     // また、環境は親となる環境からすべて繋がっており、環境の内部にはさらに
42     // 別の環境を含むが、環境を利用する側からはそれが見えないようにしている。
43
44     interpreter::Environment env;
45
46     smart_ptr<type::Type> hash(new type::Type("hoge"));
47     smart_ptr<data::DataEntity> e(new String("hoge"));
48     smart_ptr<type::TypeDescripter> t(new type::TypeDescripter(*hash, e));
49     smart_ptr<data::DataSpace> ds(new data::DataSpace(t));
50     smart_ptr<data::Object> o(new data::Object(false, ds));
51
52     // 単体のenvironmentに束縛を追加する。
53     // environmentに束縛を追加するには、symbolを利用しなければならないが、
54     // 実際の利便性としては、symbolそのものよりも、symbol->stringで取得
55     // 出来る文字列からアクセス出来たほうが楽。
56     // 実際には、この追加される束縛は全てGCから返されたsmart_ptrになる。
57     env.append("name", o);
58
59     // 実際にはの部分はDataCastingHelperで処理が隠蔽される。
60     unicode::UniString* tmp = reinterpret_cast<unicode::UniString*>(
61         env["name"]->getSpace()->getTypeDescripter().getEntity()->getAddress());
62     
63     asserter->check((*tmp)[0].getRawCode(), 'h');
64     asserter->check(env["name"]->enableChange(), false);
65     
66     return asserter->isOk();
67 }
68
69 int main(int argc, char *argv[])
70 {
71     simpletest::SimpleTestSuite suite("environment test");
72     suite.addTester(sfcr::screate(environment_test, suite.getAsserter()));
73     suite.run();
74     return 0;
75 }