source: anotherchoice/tags/jsp-1.4.4-full-UTF8/cfg/base/collection.cpp@ 26

Last change on this file since 26 was 26, checked in by ykominami, 12 years ago

initial

File size: 31.1 KB
Line 
1/*
2 * TOPPERS/JSP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
5 *
6 * Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 *
9 * 上記著作権者
10は,以下の (1)〜(4) の条件か,Free Software Foundation
11 * によってå…
12¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
13 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
14 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
15å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
16 * 利用と呼ぶ)することを無償で許諾する.
17 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
18 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
19 * スコード中に含まれていること.
20 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
21 * 用できる形で再é…
22å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
23å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
24 * 者
25マニュアルなど)に,上記の著作権表示,この利用条件および下記
26 * の無保証規定を掲載すること.
27 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
28 * 用できない形で再é…
29å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®ã„ずれかの条件を満たすこ
30 * と.
31 * (a) 再é…
32å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
33マニュアルなど)に,上記の著
34 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
35 * (b) 再é…
36å¸ƒã®å½¢æ…
37‹ã‚’,別に定める方法によって,TOPPERSプロジェクトに
38 * 報告すること.
39 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
40 * 害からも,上記著作権者
41およびTOPPERSプロジェクトをå…
42è²¬ã™ã‚‹ã“と.
43 *
44 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
45お
46 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
47 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
48 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
49 *
50 * @(#) $Id: collection.cpp,v 1.3 2003/12/15 07:32:13 takayuki Exp $
51 */
52
53// $Header: /home/CVS/configurator/base/collection.cpp,v 1.3 2003/12/15 07:32:13 takayuki Exp $
54
55#include "base/collection.h"
56
57
58using namespace std;
59
60 //特に何もしないコンストラクタ
61Collection::Collection(void) throw()
62{}
63
64 //デストラクタ
65Collection::~Collection(void) throw()
66{
67 //念のために例外にふたをする
68 try { clear(); }
69 catch(...) {}
70}
71
72 //インスタンスをコレクションに追加 (順序は登録順)
73bool Collection::addInstance(Collectable * _instance, bool _destruction) throw()
74{
75 bool result = false;
76
77 if(isValid() && _instance != 0) {
78
79 // 同じインスタンスを2回登録しないようにする
80 list<Element>::iterator scope;
81
82 scope = container.begin();
83 while(scope != container.end()) {
84 if(scope->instance == _instance)
85 break;
86 ++ scope;
87 }
88
89 if(scope == container.end()) {
90 // 末尾に要素を追加
91 struct Element element;
92 element.instance = _instance;
93 element.destruction = _destruction;
94 container.push_back(element);
95 }
96 else {
97 // 破棄指示は最新に直しておく
98 scope->destruction = _destruction;
99 }
100
101 result = true;
102 }
103
104 return result;
105}
106
107 //指定されたCollectableインスタンスに相当する場所を指すイテレータの取得 (getInstance)
108list<Collection::Element>::const_iterator Collection::_findInstance(const Collectable * predecessor) const throw()
109{
110 list<Element>::const_iterator result;
111
112 result = container.begin();
113
114 //predecessorが指定されていた場合は探す
115 if(predecessor != 0) {
116 while(result != container.end()) {
117 if(result->instance == predecessor) {
118 ++ result; //検索はpredecessorの次から
119 break;
120 }
121 ++ result;
122 }
123 }
124
125 return result;
126}
127
128 //コレクションからインスタンスを取得
129Collectable * Collection::getInstance(const type_info & type, const Collectable * predecessor) const throw()
130{
131 Collectable * result;
132
133 result = 0;
134 if(isValid()) {
135
136 list<Element>::const_iterator scope;
137
138 // å…
139ˆè¡Œã™ã‚‹ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã®å ´æ‰€ã‚’探す
140 scope = _findInstance(predecessor);
141
142 // 指定された型を持つ次のインスタンスを探す
143 while(scope != container.end()) {
144 if(typeid(*scope->instance) == type) {
145 result = scope->instance;
146 break;
147 }
148 ++ scope;
149 }
150 }
151
152 return result;
153}
154
155 //コレクションからインスタンスを除外 (破棄はしない)
156bool Collection::removeInstance(const Collectable * instance) throw()
157{
158 bool result = false;
159
160 if(isValid() && instance != 0) {
161 list<Element>::iterator scope;
162
163 scope = container.begin();
164 while(scope != container.end()) {
165 if(scope->instance == instance) {
166 //要素の削除
167 container.erase(scope);
168 result = true;
169 break;
170 }
171 ++ scope;
172 }
173 }
174
175 return result;
176}
177
178 //å…
179¨è¦ç´ ã®ç ´æ£„
180void Collection::clear(void)
181{
182 if(isValid()) {
183 list<Element>::iterator scope;
184 list<Element>::iterator next;
185
186 //破棄フラグがついているインスタンスを破棄
187 scope = container.begin();
188 while(scope != container.end()) {
189
190 next = scope; //ROT系はdeleteすると自分を消しにかかるので、イテレータを保存するためにå…
191ˆã«æ¬¡ã‚’取得しておく
192 ++ next;
193
194 if(scope->destruction)
195 delete scope->instance;
196
197 scope = next;
198 }
199
200 container.clear();
201 }
202}
203
204/************************************************* テストスィート *************************************************/
205
206#ifdef TESTSUITE
207#include "coverage_undefs.h"
208
209class TestCollectable : public Collectable
210{};
211
212class TestCollectable2 : public Collectable
213{ public: ~TestCollectable2(void) throw() { TestSuite::check("TestCollectable2::~TestCollectable2"); } };
214
215#ifdef _MSC_VER
216class TestCollectable3 : public Collectable
217{ public: ~TestCollectable3(void) throw(int) { throw 0; } };
218#endif
219
220class TestCollectable4 : public TestCollectable2
221{ public: ~TestCollectable4(void) throw() { TestSuite::check("TestCollectable4::~TestCollectable4"); } };
222
223TESTSUITE(main, Collection)
224{
225 BEGIN_CASE("addInstance","addInstance") {
226 BEGIN_CASE("1","インスタンスは正しく登録できる") {
227 Collection col;
228 TestCollectable test;
229 TestCollectable2 test2;
230
231 BEGIN_CASE("1","登録できTRUEが返る (1個目)") {
232 if(!col.addInstance(&test, false))
233 TEST_FAIL;
234 } END_CASE;
235
236 BEGIN_CASE("2","登録できtrueが返る (2個目)") {
237 if(!col.addInstance(&test2, false))
238 TEST_FAIL;
239 } END_CASE;
240
241 BEGIN_CASE("3","個数が合ってる") {
242 if(col.container.size() != 2)
243 TEST_FAIL;
244 } END_CASE;
245
246 BEGIN_CASE("4","順番があってる") {
247 list<Element>::iterator scope;
248 scope = col.container.begin();
249 if(scope->instance != &test || scope->destruction != false)
250 TEST_FAIL;
251 ++ scope;
252 if(scope->instance != &test2 || scope->destruction != false)
253 TEST_FAIL;
254 ++ scope;
255 if(scope != col.container.end())
256 TEST_FAIL;
257 } END_CASE;
258 } END_CASE;
259
260 BEGIN_CASE("2","NULLインスタンスは登録されない") {
261 Collection col;
262
263 BEGIN_CASE("1","NULLインスタンスを渡すとfalseが返る") {
264 if(col.addInstance(0))
265 TEST_FAIL;
266 } END_CASE;
267
268 BEGIN_CASE("2","何も登録されていない") {
269 if(col.container.size() != 0)
270 TEST_FAIL;
271 } END_CASE;
272 } END_CASE;
273
274 BEGIN_CASE("3","無効なインスタンスに対する操作でfalseが返る") {
275 Collection col;
276
277 if(col.addInstance(0))
278 TEST_FAIL;
279 } END_CASE;
280
281 BEGIN_CASE("4","同一のインスタンスを2回登録しても1個だけしか登録されない") {
282 Collection col;
283 TestCollectable test;
284
285 TEST_CASE("0","[前提] 一回目は正しく成功する", col.addInstance(&test, false));
286
287 TEST_CASE("1","関数は正しく終了する", col.addInstance(&test, false));
288 TEST_CASE("2","インスタンス数は増えていない", col.container.size() == 1);
289
290 } END_CASE;
291
292 } END_CASE;
293
294 BEGIN_CASE("getInstance(type_info)","getInstance(type_info)") {
295 Collection col;
296 TestCollectable test;
297 TestCollectable test_2;
298 TestCollectable test_3;
299 TestCollectable2 test2;
300 TestCollectable2 test2_2;
301 TestCollectable2 test2_3;
302
303 col.addInstance(test);
304 col.addInstance(test2);
305 col.addInstance(test_2);
306 col.addInstance(test2_2);
307 col.addInstance(test_3);
308 col.addInstance(test2_3);
309
310 Collectable * instance = 0;
311
312 BEGIN_CASE("1","最初の要素を取得できる") {
313 instance = col.getInstance(typeid(TestCollectable));
314 if(instance != &test)
315 TEST_FAIL;
316 } END_CASE;
317
318 BEGIN_CASE("2","次の要素を取得できる") {
319 instance = col.getInstance(typeid(TestCollectable), instance);
320 if(instance != &test_2)
321 TEST_FAIL;
322 } END_CASE;
323
324 BEGIN_CASE("3","さらに次の要素を取得できる") {
325 instance = col.getInstance(typeid(TestCollectable), instance);
326 if(instance != &test_3)
327 TEST_FAIL;
328 } END_CASE;
329
330 BEGIN_CASE("4","そのまた次の要素を取得したらNULLが返る") {
331 instance = col.getInstance(typeid(TestCollectable), instance);
332 if(instance != 0)
333 TEST_FAIL;
334 } END_CASE;
335
336 BEGIN_CASE("5","最初の要素を取得できる") {
337 instance = col.getInstance(typeid(TestCollectable2));
338 if(instance != &test2)
339 TEST_FAIL;
340 } END_CASE;
341
342 BEGIN_CASE("6","次の要素を取得できる") {
343 instance = col.getInstance(typeid(TestCollectable2), instance);
344 if(instance != &test2_2)
345 TEST_FAIL;
346 } END_CASE;
347
348 BEGIN_CASE("7","さらに次の要素を取得できる") {
349 instance = col.getInstance(typeid(TestCollectable2), instance);
350 if(instance != &test2_3)
351 TEST_FAIL;
352 } END_CASE;
353
354 BEGIN_CASE("8","そのまた次の要素を取得したらNULLが返る") {
355 instance = col.getInstance(typeid(TestCollectable), instance);
356 if(instance != 0)
357 TEST_FAIL;
358 } END_CASE;
359
360 BEGIN_CASE("9","基底クラスではひっかからない") {
361 instance = col.getInstance(typeid(Collectable));
362 if(instance != 0)
363 TEST_FAIL;
364 } END_CASE;
365
366 TEST_CASE("10","NULLに対して実行するとNULLが返る", ((Collection *)0)->getInstance(typeid(Collectable)) == 0);
367 } END_CASE;
368
369 BEGIN_CASE("getInstance<T>()","getInstance<T>()") {
370 Collection col;
371 TestCollectable test;
372 TestCollectable test_2;
373 TestCollectable test_3;
374 TestCollectable2 test2;
375 TestCollectable2 test2_2;
376 TestCollectable2 test2_3;
377
378 col.addInstance(test);
379 col.addInstance(test2);
380 col.addInstance(test_2);
381 col.addInstance(test2_2);
382 col.addInstance(test_3);
383 col.addInstance(test2_3);
384
385 Collectable * instance = 0;
386
387 BEGIN_CASE("1","最初の要素を取得できる") {
388 instance = col.getInstance<TestCollectable>();
389 if(instance != &test)
390 TEST_FAIL;
391 } END_CASE;
392
393 BEGIN_CASE("2","次の要素を取得できる") {
394 instance = col.getInstance<TestCollectable>(instance);
395 if(instance != &test_2)
396 TEST_FAIL;
397 } END_CASE;
398
399 BEGIN_CASE("3","さらに次の要素を取得できる") {
400 instance = col.getInstance<TestCollectable>(instance);
401 if(instance != &test_3)
402 TEST_FAIL;
403 } END_CASE;
404
405 BEGIN_CASE("4","そのまた次の要素を取得したらNULLが返る") {
406 instance = col.getInstance<TestCollectable>(instance);
407 if(instance != 0)
408 TEST_FAIL;
409 } END_CASE;
410
411 BEGIN_CASE("5","最初の要素を取得できる") {
412 instance = col.getInstance<TestCollectable2>();
413 if(instance != &test2)
414 TEST_FAIL;
415 } END_CASE;
416
417 BEGIN_CASE("6","次の要素を取得できる") {
418 instance = col.getInstance<TestCollectable2>(instance);
419 if(instance != &test2_2)
420 TEST_FAIL;
421 } END_CASE;
422
423 BEGIN_CASE("7","さらに次の要素を取得できる") {
424 instance = col.getInstance<TestCollectable2>(instance);
425 if(instance != &test2_3)
426 TEST_FAIL;
427 } END_CASE;
428
429 BEGIN_CASE("8","そのまた次の要素を取得したらNULLが返る") {
430 instance = col.getInstance<TestCollectable2>(instance);
431 if(instance != 0)
432 TEST_FAIL;
433 } END_CASE;
434
435 BEGIN_CASE("9","基底クラスで引っ掛ける その1") {
436 instance = col.getInstance<Collectable>();
437 if(instance != &test)
438 TEST_FAIL;
439 } END_CASE;
440
441 BEGIN_CASE("10","基底クラスで引っ掛ける その2") {
442 instance = col.getInstance<Collectable>(instance);
443 if(instance != &test2)
444 TEST_FAIL;
445 } END_CASE;
446
447 BEGIN_CASE("11","基底クラスで引っ掛ける その3") {
448 instance = col.getInstance<Collectable>(instance);
449 if(instance != &test_2)
450 TEST_FAIL;
451 } END_CASE;
452
453 BEGIN_CASE("12","基底クラスで引っ掛ける その4") {
454 instance = col.getInstance<Collectable>(instance);
455 if(instance != &test2_2)
456 TEST_FAIL;
457 } END_CASE;
458
459 BEGIN_CASE("13","基底クラスで引っ掛ける その5") {
460 instance = col.getInstance<Collectable>(instance);
461 if(instance != &test_3)
462 TEST_FAIL;
463 } END_CASE;
464
465 BEGIN_CASE("14","基底クラスで引っ掛ける その6") {
466 instance = col.getInstance<Collectable>(instance);
467 if(instance != &test2_3)
468 TEST_FAIL;
469 } END_CASE;
470
471 BEGIN_CASE("15","基底クラスで引っ掛ける その7") {
472 instance = col.getInstance<Collectable>(instance);
473 if(instance != 0)
474 TEST_FAIL;
475 } END_CASE;
476
477 TEST_CASE("16","NULLに対して実行するとNULLが返る", ((Collection *)0)->getInstance<Collectable>() == 0);
478
479 } END_CASE;
480
481 BEGIN_CASE("removeInstance","removeInstance") {
482
483 BEGIN_CASE("1","正常に削除できる") {
484 Collection col;
485 TestCollectable test;
486 TestCollectable test2;
487
488 col.addInstance(test);
489 col.addInstance(test2);
490
491 BEGIN_CASE("1","存在する要素をremoveしたらtrue") {
492 if(!col.removeInstance(&test))
493 TEST_FAIL;
494 } END_CASE;
495
496 BEGIN_CASE("2","ちゃんと消えている") {
497 if(col.container.size() != 1 || col.container.begin()->instance != &test2)
498 TEST_FAIL;
499 } END_CASE;
500 } END_CASE;
501
502 BEGIN_CASE("2","NULLインスタンスに実行するとfalse") {
503 if(((Collection *)0)->removeInstance(0))
504 TEST_FAIL;
505 } END_CASE;
506
507 BEGIN_CASE("3","登録していないインスタンスを指定すると失敗する") {
508 Collection col;
509 TestCollectable test;
510 TestCollectable test2;
511
512 col.addInstance(test);
513
514 if(col.removeInstance(&test2))
515 TEST_FAIL;
516 } END_CASE;
517
518 BEGIN_CASE("4","NULLインスタンスを渡すと失敗する") {
519 Collection col;
520 TestCollectable test;
521 col.addInstance(test);
522
523 if(col.removeInstance(0))
524 TEST_FAIL;
525 } END_CASE;
526 } END_CASE;
527
528 BEGIN_CASE("clear","clear") {
529 BEGIN_CASE("1","実体を登録してclear") {
530 Collection col;
531 TestCollectable2 test2;
532
533 TestSuite::clearCheckpoints();
534 col.addInstance(test2);
535 col.clear();
536
537 BEGIN_CASE("1","要素がなくなっている") {
538 if(col.container.size() != 0)
539 TEST_FAIL;
540 } END_CASE;
541
542 TEST_CASE("2","test2は破棄されない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
543 } END_CASE;
544
545 BEGIN_CASE("2","ポインタを登録してclear") {
546 Collection col;
547 TestCollectable2 * test2 = new TestCollectable2;
548
549 TestSuite::clearCheckpoints();
550 col.addInstance(test2);
551 col.clear();
552
553 BEGIN_CASE("1","要素がなくなっている") {
554 if(col.container.size() != 0)
555 TEST_FAIL;
556 } END_CASE;
557
558 TEST_CASE("2","test2は破棄される", TestSuite::isReached("TestCollectable2::~TestCollectable2"));
559 } END_CASE;
560
561 BEGIN_CASE("3","NULLオブジェクトに対して実行しても暴走しない") {
562 ((Collection *)0)->clear();
563 } END_CASE;
564
565#ifdef _MSC_VER
566 BEGIN_CASE("4","例外が出るようなデストラクタをもつインスタンスを破棄すると例外が漏れる") {
567 Collection col;
568 bool result = false;
569 TestCollectable3 * test = new TestCollectable3;
570
571 col.addInstance(test);
572 try {
573 col.clear();
574 }
575 catch(...) {
576 result = true;
577 }
578
579
580 if(!result)
581 TEST_FAIL;
582 } END_CASE;
583#endif
584 } END_CASE;
585
586 BEGIN_CASE("destructor","destructor") {
587 BEGIN_CASE("1","実体を登録して破棄") {
588 Collection * col = new Collection;
589 TestCollectable2 test2;
590
591 TestSuite::clearCheckpoints();
592 col->addInstance(test2);
593 delete col;
594
595 TEST_CASE("1","test2は破棄されない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
596 } END_CASE;
597
598 BEGIN_CASE("2","ポインタを登録してclear") {
599 Collection * col = new Collection;
600 TestCollectable2 * test2 = new TestCollectable2;
601
602 TestSuite::clearCheckpoints();
603 col->addInstance(test2);
604 delete col;
605
606 TEST_CASE("1","test2は破棄される", TestSuite::isReached("TestCollectable2::~TestCollectable2"));
607 } END_CASE;
608
609#ifdef _MSC_VER
610 BEGIN_CASE("3","例外が出るようなデストラクタを持つインスタンスを破棄しても例外が漏れない") {
611 Collection * col = new Collection;
612 TestCollectable3 * test2 = new TestCollectable3;
613 bool result = true;
614
615 TestSuite::clearCheckpoints();
616 col->addInstance(test2);
617
618 try {
619 delete col;
620 }
621 catch(...) {
622 result = false;
623 }
624
625 if(!result)
626 TEST_FAIL;
627 } END_CASE;
628#endif
629 } END_CASE;
630
631 BEGIN_CASE("deleteInstance","deleteInstance") {
632 BEGIN_CASE("1","インスタンスを削除できる") {
633 Collection col;
634 TestCollectable2 * test = new TestCollectable2;
635
636 TestSuite::clearCheckpoints();
637 col.addInstance(test);
638
639 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance<TestCollectable2>() != 0);
640
641 col.deleteInstance<TestCollectable2>();
642 TEST_CASE("1","インスタンスが消えている", col.getInstance<TestCollectable2>() == 0);
643 TEST_CASE("2","デストラクタが実行されている", TestSuite::isReached("TestCollectable2::~TestCollectable2"));
644 }END_CASE;
645
646 BEGIN_CASE("2","関係ないクラスを指定したら削除されない") {
647 Collection col;
648 TestCollectable2 * test = new TestCollectable2;
649
650 TestSuite::clearCheckpoints();
651 col.addInstance(test);
652
653 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance<TestCollectable2>() != 0);
654
655 col.deleteInstance<TestCollectable>();
656 TEST_CASE("1","インスタンスが消えていない", col.getInstance<TestCollectable2>() != 0);
657 TEST_CASE("2","デストラクタが実行されていない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
658 delete test;
659 }END_CASE;
660
661 BEGIN_CASE("3","同じクラスだけでなく派生å…
662ˆã‚‚消える") {
663 Collection col;
664 TestCollectable2 * test = new TestCollectable2;
665 TestCollectable4 * test2 = new TestCollectable4;
666
667 TestSuite::clearCheckpoints();
668 col.addInstance(test);
669 col.addInstance(test2);
670
671 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable2)) != 0);
672 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable4)) != 0);
673
674 col.deleteInstance<TestCollectable2>();
675 TEST_CASE("1","インスタンスがすべて消えている", col.getInstance<TestCollectable2>() == 0);
676 TEST_CASE("2","TestCollectable2のデストラクタが実行されている", TestSuite::isReached("TestCollectable2::~TestCollectable2"));
677 TEST_CASE("3","TestCollectable4のデストラクタが実行されている", TestSuite::isReached("TestCollectable4::~TestCollectable4"));
678 }END_CASE;
679
680 BEGIN_CASE("4","実体は破棄対象にならない") {
681 Collection col;
682 TestCollectable2 test;
683
684 TestSuite::clearCheckpoints();
685 col.addInstance(test);
686
687 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance<TestCollectable2>() != 0);
688
689 col.deleteInstance<TestCollectable2>();
690 TEST_CASE("1","インスタンスが消えている", col.getInstance<TestCollectable2>() == 0);
691 TEST_CASE("2","デストラクタが実行されていない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
692 }END_CASE;
693
694 BEGIN_CASE("5","NULLオブジェクトに対して発行しても暴走しない") {
695 ((Collection *)0)->deleteInstance<TestCollectable>();
696 } END_CASE;
697
698 } END_CASE;
699
700 BEGIN_CASE("replaceInstance","replaceInstance") {
701 BEGIN_CASE("1","登録済みのインスタンスをå…
702¥ã‚Œæ›¿ãˆã‚‹") {
703 Collection col;
704 TestCollectable2 test;
705 TestCollectable2 test2;
706
707 TestSuite::clearCheckpoints();
708 col.addInstance(test);
709
710 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable2)) != 0);
711
712 TEST_CASE("1", "関数は成功する", col.replaceInstance<TestCollectable2>(test2));
713 TEST_CASE("2", "インスタンスはå…
714¥ã‚Œæ›¿ã‚ã£ã¦ã„ã‚‹", col.getInstance(typeid(TestCollectable2)) == &test2);
715 TEST_CASE("3", "test2以外にインスタンスはない", col.getInstance(typeid(TestCollectable2), &test2) == 0);
716 TEST_CASE("4", "デストラクタは実行されてない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
717 } END_CASE;
718
719 BEGIN_CASE("2","登録済みのインスタンスを派生クラスのインスタンスでå…
720¥ã‚Œæ›¿ãˆã‚‹") {
721 Collection col;
722 TestCollectable2 test;
723 TestCollectable4 test2;
724
725 TestSuite::clearCheckpoints();
726 col.addInstance(test);
727
728 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable2)) != 0);
729
730 TEST_CASE("1", "関数は成功する", col.replaceInstance<TestCollectable2>(test2));
731 TEST_CASE("2", "インスタンスはå…
732¥ã‚Œæ›¿ã‚ã£ã¦ã„ã‚‹", col.getInstance<TestCollectable2>() == &test2);
733 TEST_CASE("3", "test2以外にインスタンスはない", col.getInstance(typeid(TestCollectable2)) == 0);
734 TEST_CASE("4", "デストラクタは実行されてない", !TestSuite::isReached("TestCollectable2::~TestCollectable2"));
735 } END_CASE;
736
737 BEGIN_CASE("3","無関係なクラスを指定してå…
738¥ã‚Œæ›¿ãˆã‚‹") {
739 Collection col;
740 TestCollectable2 test;
741 TestCollectable4 test2;
742
743 TestSuite::clearCheckpoints();
744 col.addInstance(test);
745
746 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable2)) != 0);
747
748 TEST_CASE("1", "関数は失敗する", !col.replaceInstance<TestCollectable>(test2));
749 TEST_CASE("2", "インスタンスは残っている", col.getInstance<TestCollectable2>() == &test);
750 } END_CASE;
751
752 BEGIN_CASE("4","登録済みのインスタンスをå…
753¥ã‚Œæ›¿ãˆã‚‹ (動的確保)") {
754 Collection col;
755 TestCollectable2 * test = new TestCollectable2;
756 TestCollectable2 test2;
757
758 TestSuite::clearCheckpoints();
759 col.addInstance(test);
760
761 TEST_CASE("0","[前提] ちゃんと登録されている", col.getInstance(typeid(TestCollectable2)) != 0);
762
763 TEST_CASE("1", "関数は成功する", col.replaceInstance<TestCollectable2>(test2));
764 TEST_CASE("2", "インスタンスはå…
765¥ã‚Œæ›¿ã‚ã£ã¦ã„ã‚‹", col.getInstance(typeid(TestCollectable2)) == &test2);
766 TEST_CASE("3", "test2以外にインスタンスはない", col.getInstance(typeid(TestCollectable2), &test2) == 0);
767 TEST_CASE("4", "デストラクタは実行されてる", TestSuite::isReached("TestCollectable2::~TestCollectable2"));
768 } END_CASE;
769
770 } END_CASE;
771}
772
773
774class TestRuntimeObject : public RuntimeObject
775{
776public:
777 TestRuntimeObject(void) { CHECKPOINT("TestRuntimeObject::TestRuntimeObject"); }
778 ~TestRuntimeObject(void) { CHECKPOINT("TestRuntimeObject::~TestRuntimeObject"); }
779};
780
781class TestRuntimeObject2 : public RuntimeObject
782{
783public:
784 TestRuntimeObject2(void) : RuntimeObject(true) { CHECKPOINT("TestRuntimeObject2::TestRuntimeObject2"); }
785 ~TestRuntimeObject2(void) { CHECKPOINT("TestRuntimeObject2::~TestRuntimeObject2"); }
786};
787
788TESTSUITE(main, RuntimeObjectTable)
789{
790 Singleton<RuntimeObjectTable>::Context context;
791 Singleton<RuntimeObjectTable>::saveContext(context);
792
793 BEGIN_CASE("getInstance","getInstance") {
794 BEGIN_CASE("1","getInstanceは正しく機能する") {
795 Singleton<RuntimeObjectTable>::renewInstance();
796 TestCollectable test;
797
798 Singleton<RuntimeObjectTable>::getInstance()->addInstance(test);
799
800 BEGIN_CASE("1","getInstance(typeid)") {
801 if(RuntimeObjectTable::getInstance(typeid(TestCollectable)) != &test)
802 TEST_FAIL;
803 } END_CASE;
804
805 BEGIN_CASE("2","getInstance<T>()") {
806 if(RuntimeObjectTable::getInstance<TestCollectable>() != &test)
807 TEST_FAIL;
808 } END_CASE;
809 } END_CASE;
810 } END_CASE;
811
812 BEGIN_CASE("RuntimeObject","RuntimeObject") {
813 BEGIN_CASE("1","生成すると登録され、破棄で消滅
814する") {
815 TestRuntimeObject * ro;
816
817 TestSuite::clearCheckpoints();
818
819 TEST_CASE("0","[前提] 登録されていない", RuntimeObjectTable::getInstance<TestRuntimeObject>() == 0);
820
821 ro = new TestRuntimeObject;
822
823 TEST_CASE("1","コンストラクタが実行されている", TestSuite::isReached("TestRuntimeObject::TestRuntimeObject"));
824 TEST_CASE("2","登録されている", RuntimeObjectTable::getInstance<TestRuntimeObject>() == ro);
825
826 delete ro;
827
828 TEST_CASE("3","デストラクタが実行されている", TestSuite::isReached("TestRuntimeObject::~TestRuntimeObject"));
829 TEST_CASE("4","登録が解除されている", RuntimeObjectTable::getInstance<TestRuntimeObject>() == 0);
830
831 } END_CASE;
832
833 BEGIN_CASE("2","自動破棄のチェック") {
834 TestRuntimeObject * ro;
835 TestRuntimeObject2 * ro2;
836
837 TestSuite::clearCheckpoints();
838
839 TEST_CASE("0","[前提] 登録されていない", RuntimeObjectTable::getInstance<TestRuntimeObject>() == 0);
840 TEST_CASE("0","[前提] 登録されていない", RuntimeObjectTable::getInstance<TestRuntimeObject2>() == 0);
841
842 ro = new TestRuntimeObject;
843 ro2 = new TestRuntimeObject2;
844
845 TEST_CASE("1","コンストラクタが実行されている(ro)", TestSuite::isReached("TestRuntimeObject::TestRuntimeObject"));
846 TEST_CASE("2","コンストラクタが実行されている(ro2)", TestSuite::isReached("TestRuntimeObject2::TestRuntimeObject2"));
847 TEST_CASE("3","登録されている(ro)", RuntimeObjectTable::getInstance<TestRuntimeObject>() == ro);
848 TEST_CASE("4","登録されている(ro2)", RuntimeObjectTable::getInstance<TestRuntimeObject2>() == ro2);
849
850 Singleton<RuntimeObjectTable>::getInstance()->clear();
851
852 TEST_CASE("5","デストラクタは実行されていない (ro)", !TestSuite::isReached("TestRuntimeObject::~TestRuntimeObject"));
853 TEST_CASE("6","デストラクタは実行されている (ro2)", TestSuite::isReached("TestRuntimeObject2::~TestRuntimeObject2"));
854 TEST_CASE("7","登録が解除されている", RuntimeObjectTable::getInstance<TestRuntimeObject>() == 0);
855 TEST_CASE("8","登録が解除されている", RuntimeObjectTable::getInstance<TestRuntimeObject2>() == 0);
856
857 delete ro;
858 } END_CASE;
859
860 } END_CASE;
861
862 Singleton<RuntimeObjectTable>::restoreContext(context);
863}
864
865#endif
866
Note: See TracBrowser for help on using the repository browser.