6 #include "../simpletest.h"
8 #include "../environment.h"
9 #include "../base_object.h"
11 #include "../data_structure_interface.h"
12 #include "../data_structure.h"
13 #include "../object.h"
16 #include "../unicode.h"
20 using namespace utakata;
22 class String : public data::DataEntity
25 String(std::string str) : str_(new unicode::UniString(unicode::convert(str)))
29 virtual void* getAddress() {return reinterpret_cast<unsigned char*>(&(*str_));}
30 virtual const void* getAddress() const {return reinterpret_cast<unsigned char*>(&(*str_));}
32 smart_ptr<unicode::UniString> str_;
35 bool environment_test(smart_ptr<simpletest::SimpleTestAsserter> asserter)
37 // Schemeの「環境」として扱われるクラスのテスト。
38 // data::Environmentは、ある時点での環境を表す。
39 // 環境とは、ある時点での可視である束縛変数(キーワード含む)の集合
40 // であり、定義された時点で可視である変数を全て含む。
41 // また、環境は親となる環境からすべて繋がっており、環境の内部にはさらに
42 // 別の環境を含むが、環境を利用する側からはそれが見えないようにしている。
44 interpreter::Environment env;
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));
52 // 単体のenvironmentに束縛を追加する。
53 // environmentに束縛を追加するには、symbolを利用しなければならないが、
54 // 実際の利便性としては、symbolそのものよりも、symbol->stringで取得
55 // 出来る文字列からアクセス出来たほうが楽。
56 // 実際には、この追加される束縛は全てGCから返されたsmart_ptrになる。
57 env.append("name", o);
59 // 実際にはの部分はDataCastingHelperで処理が隠蔽される。
60 unicode::UniString* tmp = reinterpret_cast<unicode::UniString*>(
61 env["name"]->getSpace()->getTypeDescripter().getEntity()->getAddress());
63 asserter->check((*tmp)[0].getRawCode(), 'h');
64 asserter->check(env["name"]->enableChange(), false);
66 return asserter->isOk();
69 int main(int argc, char *argv[])
71 simpletest::SimpleTestSuite suite("environment test");
72 suite.addTester(sfcr::screate(environment_test, suite.getAsserter()));