ARB
test_env.h
Go to the documentation of this file.
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 
15 
16 template<typename SEQTYPE>
17 struct 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(), NULp),
23  templ(NULp),
24  permit_flag_updates(exports.get_modifying_flag_ref(), -1)
25  {}
27  delete templ;
28  }
29  void init(AliView *aliview) {
30  fake_AW_init_color_groups(); // acts like color_groups were active
31  delete templ;
32  templ = aliview->has_data() ? new SEQTYPE(aliview) : NULp;
33  AWT_graphic_parsimony::init(aliview, templ, true, false);
34  }
35 
37  // only needs to set members needed to compute_tree()
38  groupScale.pow = .33;
39  groupScale.linear = 1.0;
40  }
41 };
42 
43 
44 template<typename SEQTYPE>
45 class PARSIMONY_testenv : virtual Noncopyable {
46  GB_shell shell;
47  AP_main apMain;
48  fake_agt<SEQTYPE> *agt;
49  ArbParsimony parsimony;
50  long prev_combine_count;
51  KL_Settings *klSettings;
52 
53  void common_init(const char *dbname) {
54  apMain.open(dbname);
55 
57  ap_main = &apMain;
58 
59  agt = new fake_agt<SEQTYPE>(parsimony);
60  parsimony.set_tree(agt);
61 
62  prev_combine_count = AP_combinableSeq::combine_count();
63  }
64 
65 public:
66  PARSIMONY_testenv(const char *dbname)
67  : parsimony(),
68  klSettings(NULp)
69  {
70  common_init(dbname);
71  agt->init(new AliView(ap_main->get_gb_main()));
72  }
73  PARSIMONY_testenv(const char *dbname, const char *aliName);
75  if (root_node()) {
77  }
78 
79  TEST_EXPECT_EQUAL(ap_main, &apMain);
80  ap_main = NULp;
81 
82  delete agt;
83  delete klSettings;
84 
85  ap_assert(prev_combine_count == AP_combinableSeq::combine_count()); // please add tests documenting combines_performed()
86  }
87 
88  AP_tree_nlen *root_node() {
89  AP_tree_nlen *root = apMain.get_root_node();
90  ap_assert(root == graphic_tree()->get_root_node());
91  return root;
92  }
93  AP_pars_root *tree_root() { return agt->get_tree_root(); }
94 
95  GB_ERROR load_tree(const char *tree_name) {
97  GB_transaction ta(gb_main); // @@@ do inside AWT_graphic_tree::load?
98  GB_ERROR error = agt->load_from_DB(gb_main, tree_name);
99  if (!error) {
100  AP_tree_edge::initialize(rootNode()); // builds edges
101 
102  ap_assert(root_node());
103  ap_assert(root_node() == rootNode()); // need tree-access via global 'ap_main' (too much code is based on that)
104  ap_assert(rootEdge());
105 
107  }
108  return error;
109  }
110 
111  void push() { apMain.remember(); }
112  void push_whole_tree() { apMain.remember_whole_tree(); }
113  void pop() { apMain.revert(); }
114  void accept() { apMain.accept(); }
115  void accept_if(bool cond) { apMain.accept_if(cond); }
116 
117 #if defined(ASSERTION_USED) || defined(UNIT_TESTS)
120 #endif
121 
122  Level get_frame_level() { return apMain.get_frameLevel(); }
124 
126 
127  GBDATA *gbmain() const { return apMain.get_gb_main(); }
128 
130  long performed = AP_combinableSeq::combine_count()-prev_combine_count;
131  prev_combine_count = AP_combinableSeq::combine_count();
132  return performed;
133  }
134 
135  void compute_tree() { root_node()->compute_tree(); }
136 
138  ap_assert(klSettings);
139  return *klSettings;
140  }
141 
142 #if defined(PROVIDE_PRINT)
143  void dump2file(const char *name) { apMain.dump2file(name); }
144 #endif
145 };
146 
147 // ---------------------------
148 // expected combines
149 
150 # define TEST_EXPECT_COMBINES_PERFORMED(env,expComb) TEST_EXPECT_EQUAL((env).combines_performed(), expComb)
151 
152 
153 #else
154 #error test_env.h included twice
155 #endif // TEST_ENV_H
156 
157 
fake_agt(ArbParsimony &parsimony_)
Definition: test_env.h:21
LocallyModify< int > permit_flag_updates
Definition: test_env.h:19
bool has_data() const
Definition: AliView.hxx:50
void accept()
Definition: AP_main.cxx:205
void set_tree(AWT_graphic_parsimony *tree_)
Definition: PARS_main.cxx:66
SEQTYPE * templ
Definition: test_env.h:18
void accept_if(bool cond)
Definition: test_env.h:115
AWT_graphic_parsimony * graphic_tree()
Definition: test_env.h:125
long combines_performed()
Definition: test_env.h:129
Validity all_available_pops_will_produce_valid_trees()
Definition: test_env.h:119
GBDATA * get_gb_main() const
Validity revert_will_produce_valid_tree()
ARB_edge rootEdge(TreeRoot *root)
Definition: TreeNode.h:857
AP_pars_root * tree_root()
Definition: test_env.h:93
size_t get_user_push_counter() const
Validity all_available_reverts_will_produce_valid_trees()
static long combine_count()
AP_tree_nlen * root_node()
Definition: test_env.h:88
Level get_frame_level()
Definition: test_env.h:122
static void destroy(AP_tree_nlen *root)
GB_ERROR load_tree(const char *tree_name)
Definition: test_env.h:95
Level get_user_push_counter()
Definition: test_env.h:123
void fake_AW_init_color_groups()
void accept_if(bool cond)
~fake_agt() OVERRIDE
Definition: test_env.h:26
static void error(const char *msg)
Definition: mkptypes.cxx:96
void remember()
Definition: AP_main.cxx:50
#define ap_assert(cond)
AP_tree_nlen * rootNode()
Definition: ap_main.hxx:54
GB_ERROR open(const char *db_server)
Definition: AP_main.cxx:20
AP_main * ap_main
Definition: PARS_main.cxx:64
KL_Settings & get_KL_settings()
Definition: test_env.h:137
#define TEST_EXPECT_NULL(n)
Definition: test_unit.h:1307
xml element
GBDATA * gbmain() const
Definition: test_env.h:127
void read_tree_settings() OVERRIDE
Definition: test_env.h:36
void compute_tree()
Definition: test_env.h:135
#define OVERRIDE
Definition: cxxforward.h:93
void revert()
Definition: AP_main.cxx:66
static void initialize(AP_tree_nlen *root)
#define NULp
Definition: cxxforward.h:97
Level get_frameLevel() const
Validity pop_will_produce_valid_tree()
Definition: test_env.h:118
void init(AliView *aliview, AP_sequence *seq_prototype, bool link_to_database_, bool insert_delete_cbs)
GB_transaction ta(gb_var)
GBDATA * gb_main
Definition: adname.cxx:33
#define ASSERT_VALID_TREE(tree)
Definition: TreeNode.h:606
GBDATA * get_gb_main(DbSel db)
Definition: merge.hxx:88
#define TEST_EXPECT_EQUAL(expr, want)
Definition: test_unit.h:1283
void init(AliView *aliview)
Definition: test_env.h:29
void push_whole_tree()
Definition: test_env.h:112
unsigned long Level
Definition: AP_buffer.hxx:126
PARSIMONY_testenv(const char *dbname)
Definition: test_env.h:66