Swarm-NG  1.1
unit_tests.cpp
1 #include <cassert>
2 #include <iostream>
3 using namespace std;
4 
5 #define GENERIC
6 
7 #include "swarm/gpu/pair_calculation.hpp"
8 
9 
10 template<int nbod>
11 struct PairTest {
12  bool table[nbod][nbod];
13 
18  PairTest() {
19  for(int i = 0; i < nbod; i++)
20  for(int j = 0; j < nbod; j++)
21  table[i][j] = (i == j);
22  }
23 
24  void print_table(){
25  cout << " ";
26  for(int j = 0; j < nbod; j++) cout << "-";
27  cout << endl;
28  for(int i = 0; i < nbod; i++) {
29  cout << "|";
30  for(int j = 0; j < nbod; j++)
31  cout << (table[i][j] ? "*" : " ");
32  cout << "|\n";
33  }
34  cout << " ";
35  for(int j = 0; j < nbod; j++) cout << "-";
36  cout << endl;
37  }
38 
39  void run(){
40  for(int ij = 0; ij < nbod * (nbod-1) /2 ; ij++) {
41  int f = first<nbod>(ij), s= second<nbod>(ij);
42  assert(f >= 0 && f < nbod); assert(s >= 0 && s < nbod);
43  cout << f << ", " << s << endl;
44 
45  // Cross of the pair s,f and f,s from the table
46  table[s][f] = table[f][s] = true;
47  }
48  }
49 
50  bool pass(){
51  bool ret = true;
52  for(int i = 0; i < nbod; i++)
53  for(int j = 0; j < nbod; j++)
54  ret = ret && table[i][j];
55  return ret;
56  }
57 
58  bool test(){
59  run();
60  print_table();
61  return pass();
62  }
63 };
64 
65 
66 int main() {
67  bool p =
68  PairTest<3>().test() &&
69  PairTest<4>().test() &&
70  PairTest<5>().test() &&
71  PairTest<6>().test() &&
72  PairTest<7>().test() &&
73  PairTest<8>().test() &&
74  PairTest<9>().test() &&
75  true;
76 
77  cout << "Test " << (p ? "pass" : "failed") << endl;
78  return p ? 0 : 1;
79 }