source: tags/ms_r17q1/PARSIMONY/test_env.h

Last change on this file was 15691, checked in by westram, 7 years ago
  • reintegrates 'keys' into 'trunk'
    • implements part of #687 (species traversal via keys)
    • adds temp. unfolding of selected species
    • several fixes to [15665] (PARSIMONY)
  • adds: log:branches/keys@15591:15690
File size: 5.5 KB
Line 
1// ================================================================= //
2//                                                                   //
3//   File      : test_env.h                                          //
4//   Purpose   : parsimony unittest toolkit                          //
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 FINAL_TYPE : public AWT_graphic_parsimony, virtual Noncopyable {
18    SEQTYPE *templ;
19    LocallyModify<int> permit_flag_updates; // w/o need for AWT_auto_refresh instances in unit tests
20
21    fake_agt(ArbParsimony& parsimony_)
22        : AWT_graphic_parsimony(parsimony_, ap_main->get_gb_main(), NULL),
23          templ(NULL),
24          permit_flag_updates(exports.get_modifying_flag_ref(), -1)
25    {
26    }
27    ~fake_agt() {
28        delete templ;
29    }
30    void init(AliView *aliview) {
31        fake_AW_init_color_groups(); // acts like color_groups were active
32        delete templ;
33        templ = aliview->has_data() ? new SEQTYPE(aliview) : NULL;
34        AWT_graphic_parsimony::init(aliview, templ, true, false);
35    }
36};
37
38
39template<typename SEQTYPE>
40class PARSIMONY_testenv : virtual Noncopyable {
41    GB_shell           shell;
42    AP_main            apMain;
43    fake_agt<SEQTYPE> *agt;
44    ArbParsimony       parsimony;
45    long               prev_combine_count;
46    KL_Settings       *klSettings;
47
48    void common_init(const char *dbname) {
49        apMain.open(dbname);
50
51        TEST_EXPECT_NULL(ap_main);
52        ap_main = &apMain;
53
54        agt = new fake_agt<SEQTYPE>(parsimony);
55        parsimony.set_tree(agt);
56
57        prev_combine_count = AP_sequence::combine_count();
58    }
59
60public:
61    PARSIMONY_testenv(const char *dbname)
62        : parsimony(),
63          klSettings(NULL)
64    {
65        common_init(dbname);
66        agt->init(new AliView(ap_main->get_gb_main()));
67    }
68    PARSIMONY_testenv(const char *dbname, const char *aliName);
69    ~PARSIMONY_testenv() {
70        if (root_node()) {
71            AP_tree_edge::destroy(root_node());
72        }
73
74        TEST_EXPECT_EQUAL(ap_main, &apMain);
75        ap_main = NULL;
76
77        delete agt;
78        delete klSettings;
79
80        ap_assert(prev_combine_count == AP_sequence::combine_count()); // please add tests documenting combines_performed()
81    }
82
83    AP_tree_nlen *root_node() {
84        AP_tree_nlen *root = apMain.get_root_node();
85        ap_assert(root == graphic_tree()->get_root_node());
86        return root;
87    }
88    AP_pars_root *tree_root() { return agt->get_tree_root(); }
89
90    GB_ERROR load_tree(const char *tree_name) {
91        GBDATA         *gb_main = ap_main->get_gb_main();
92        GB_transaction  ta(gb_main);     // @@@ do inside AWT_graphic_tree::load?
93        GB_ERROR        error   = agt->load_from_DB(gb_main, tree_name);
94        if (!error) {
95            AP_tree_edge::initialize(rootNode());   // builds edges
96
97            ap_assert(root_node());
98            ap_assert(root_node() == rootNode()); // need tree-access via global 'ap_main' (too much code is based on that)
99            ap_assert(rootEdge());
100
101            ASSERT_VALID_TREE(root_node());
102        }
103        return error;
104    }
105
106    void push() { apMain.remember(); }
107    void push_whole_tree() { apMain.remember_whole_tree(); }
108    void pop() { apMain.revert(); }
109    void accept() { apMain.accept(); }
110    void accept_if(bool cond) { apMain.accept_if(cond); }
111
112#if defined(ASSERTION_USED) || defined(UNIT_TESTS)
113    Validity pop_will_produce_valid_tree() { return apMain.revert_will_produce_valid_tree(); }
114    Validity all_available_pops_will_produce_valid_trees() { return apMain.all_available_reverts_will_produce_valid_trees(); }
115#endif
116
117    Level get_frame_level() { return apMain.get_frameLevel(); }
118    Level get_user_push_counter() { return apMain.get_user_push_counter(); }
119
120    AWT_graphic_parsimony *graphic_tree() { return agt; }
121
122    GBDATA *gbmain() const { return apMain.get_gb_main(); }
123
124    long combines_performed() {
125        long performed     = AP_sequence::combine_count()-prev_combine_count;
126        prev_combine_count = AP_sequence::combine_count();
127        return performed;
128    }
129
130    void compute_tree() { root_node()->compute_tree(); }
131
132    KL_Settings& get_KL_settings() {
133        ap_assert(klSettings);
134        return *klSettings;
135    }
136
137#if defined(PROVIDE_PRINT)
138    void dump2file(const char *name) { apMain.dump2file(name); }
139#endif
140};
141
142// ---------------------------
143//      expected combines
144//
145// Note: if code is compiled with AddressSanitizer, the number of performed combines differs (at some places).
146// I have no idea why that happens and since the other test-results show no effect, strange behavior is tolerated.
147#if defined(LEAKS_SANITIZED)
148# define TEST_EXPECT_COMBINES_PERFORMED_DIFFERS_IF_SANITIZED(env,expComb,expCombSanitize) TEST_EXPECT_EQUAL__BROKEN((env).combines_performed(), expComb, expCombSanitize)
149#else // !LEAKS_SANITIZED
150# define TEST_EXPECT_COMBINES_PERFORMED_DIFFERS_IF_SANITIZED(env,expComb,expCombSanitize) TEST_EXPECT_EQUAL((env).combines_performed(), expComb)
151#endif
152# define TEST_EXPECT_COMBINES_PERFORMED(env,expComb) TEST_EXPECT_EQUAL((env).combines_performed(), expComb)
153
154
155#else
156#error test_env.h included twice
157#endif // TEST_ENV_H
158
159
Note: See TracBrowser for help on using the repository browser.