aboutsummaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/tests.h
blob: 9cd7a10ab487fe9783957f0a482c7e9b2f1410d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <cstring>
#include <csignal>
#include <cstdio>
#include <cstdint>

class BaseTest
{
private:
   BaseTest(const BaseTest&);
   BaseTest &operator=(const BaseTest&);

   int totallen;
   bool _allOkFlag;
public:
   int _argc;
   char **_argv;

   const char *okString;
   const char *failString;

   BaseTest ()
     : totallen(70),
       _allOkFlag(true),
       _argc(0),
       _argv(nullptr),
       okString("SUCCESS"),
       failString("FAILURE")
   {
   }

   virtual int Main() = 0;

   int Entry(int argc, char **argv) {
     _argc = argc;
     _argv = argv;
     return Main();
   }

   virtual ~BaseTest() {};

   bool allWasOk() const { return _allOkFlag; }

   void PrintSeparator ()
   {
      for(int i=0; i<totallen; i++) printf("-");
      printf("\n");
   }

   virtual void PrintProgress (char *string)
   {
      printf("%s", string);
   }
#define MAX_STR_LEN 3000
   bool Progress (bool result, const char *str)
   {
      char string[MAX_STR_LEN];
      snprintf(string, sizeof(string), "%s: %s\n",
         result ? okString : failString, str);
      PrintProgress(string);
      if (! result) { _allOkFlag = false; }
      return result;
   }

   bool Progress (bool result, const char *str, int d1)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, d1);
      return Progress(result, string);
   }

   bool Progress (bool result, const char *str, int d1, int d2)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, d1, d2);
      return Progress(result, string);
   }

   bool Progress (bool result, const char *str, const char *s1)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, s1);
      return Progress(result, string);
   }

   bool Progress (bool result, const char *str, const char *s1, const char *s2)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, s1, s2);
      return Progress(result, string);
   }

   bool Progress (bool result, const char *str, const char *s1, int d1)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, s1, d1);
      return Progress(result, string);
   }

   bool Progress (bool result, const char *str, int d1, const char *s1)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, d1, s1);
      return Progress(result, string);
   }

   bool ProgressI64 (bool result, const char *str, int64_t val)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, val);
      return Progress(result, string);
   }

   bool ProgressFloat (bool result, const char *str, float val)
   {
      char string[MAX_STR_LEN-100];
      snprintf(string, sizeof(string), str, val);
      return Progress(result, string);
   }

   void Ok (const char *string)
   {
      Progress(true, string);
   }

   void Fail (const char *string)
   {
      Progress(false, string);
   }

   void TestHeader (const char *string)
   {
      int len = strlen(string);
      int leftspace = (totallen - len)/2 - 2;
      int rightspace = totallen - 4 - len - leftspace;
      int i;

      printf("\n\n");
      for(i=0; i<totallen; i++) printf("*");
      printf("\n**");
      for(i=0; i<leftspace; i++) printf(" ");   //forgot printf-specifier..
      printf("%s", string);
      for(i=0; i<rightspace; i++) printf(" ");
      printf("**\n");
      for(i=0; i<totallen; i++) printf("*");
      printf("\n");
   }
};