source: tags/testbuild/PARSIMONY/test_env.h

Last change on this file was 13267, checked in by westram, 10 years ago
  • renamed AP_main::push/pop/clear into remember/revert/accept
  • added convenience wrappers accept_if/revert_if
File size: 4.1 KB
Line 
1// ================================================================= //
2//                                                                   //
3//   File      : test_env.h                                          //
4//   Purpose   :                                                     //
5//                                                                   //
6//   Coded by Ralf Westram (coder@reallysoft.de) in September 2014   //
7//   http://www.arb-home.de/                                         //
8//                                                                   //
9// ================================================================= //
10
11#ifndef TEST_ENV_H
12#define TEST_ENV_H
13
14void fake_AW_init_color_groups();
15
16template<typename SEQTYPE>
17struct fake_agt : public AWT_graphic_parsimony, virtual Noncopyable {
18    SEQTYPE *templ;
19
20    fake_agt(ArbParsimony& parsimony_)
21        : AWT_graphic_parsimony(parsimony_, ap_main->get_gb_main(), NULL),
22          templ(NULL)
23    {
24    }
25    ~fake_agt() {
26        delete templ;
27    }
28    void init(AliView *aliview) {
29        fake_AW_init_color_groups(); // acts like no species has a color
30        delete templ;
31        templ = aliview->has_data() ? new SEQTYPE(aliview) : NULL;
32        AWT_graphic_tree::init(new AP_TreeNlenNodeFactory, aliview, templ, true, false);
33    }
34};
35
36
37template<typename SEQTYPE>
38class PARSIMONY_testenv : virtual Noncopyable {
39    GB_shell           shell;
40    AP_main            apMain;
41    fake_agt<SEQTYPE> *agt;
42    ArbParsimony       parsimony;
43    long               prev_combine_count;
44
45    void common_init(const char *dbname) {
46        apMain.open(dbname);
47
48        TEST_EXPECT_NULL(ap_main);
49        ap_main = &apMain;
50
51        agt = new fake_agt<SEQTYPE>(parsimony);
52        parsimony.set_tree(agt);
53
54        prev_combine_count = AP_sequence::combine_count();
55    }
56
57public:
58    PARSIMONY_testenv(const char *dbname)
59        : parsimony()
60    {
61        common_init(dbname);
62        agt->init(new AliView(ap_main->get_gb_main()));
63    }
64    PARSIMONY_testenv(const char *dbname, const char *aliName);
65    ~PARSIMONY_testenv() {
66        if (root_node()) {
67            AP_tree_edge::destroy(root_node());
68        }
69
70        TEST_EXPECT_EQUAL(ap_main, &apMain);
71        ap_main = NULL;
72
73        delete agt;
74
75        ap_assert(prev_combine_count == AP_sequence::combine_count()); // please add tests documenting combines_performed()
76    }
77
78    AP_tree_nlen *root_node() { return apMain.get_root_node(); }
79    AP_pars_root *tree_root() { return agt->get_tree_root(); }
80
81    GB_ERROR load_tree(const char *tree_name) {
82        GBDATA         *gb_main = ap_main->get_gb_main();
83        GB_transaction  ta(gb_main);     // @@@ do inside AWT_graphic_tree::load?
84        GB_ERROR        error   = agt->load(gb_main, tree_name, 0, 0);
85        if (!error) {
86            AP_tree_edge::initialize(rootNode());   // builds edges
87
88            ap_assert(root_node());
89            ap_assert(root_node() == rootNode()); // need tree-access via global 'ap_main' (too much code is based on that)
90            ap_assert(rootEdge());
91
92            TEST_ASSERT_VALID_TREE(root_node());
93        }
94        return error;
95    }
96
97    void push() { apMain.remember(); }
98    void pop() { apMain.revert(); }
99
100    Level get_frame_level() { return apMain.get_frameLevel(); }
101    Level get_user_push_counter() { return apMain.get_user_push_counter(); }
102
103    AWT_graphic_parsimony *graphic_tree() { return agt; }
104
105    GBDATA *gbmain() const { return ap_main->get_gb_main(); }
106
107    long combines_performed() {
108        long performed     = AP_sequence::combine_count()-prev_combine_count;
109        prev_combine_count = AP_sequence::combine_count();
110        return performed;
111    }
112
113#if defined(PROVIDE_PRINT)
114    void dump2file(const char *name) {
115        static int counter = 0;
116
117        if (counter == 0) {
118            system("rm $ARBHOME/[0-9][0-9]_*.log");
119        }
120
121        char *numbered_name = GBS_global_string_copy("%02i_%s.log", ++counter, name);
122        apMain.print2file(numbered_name);
123        free(numbered_name);
124    }
125#endif
126};
127
128
129#else
130#error test_env.h included twice
131#endif // TEST_ENV_H
132
Note: See TracBrowser for help on using the repository browser.