Browse Source

enhance test : range

nas 3 years ago
parent
commit
58f000b207
3 changed files with 95 additions and 53 deletions
  1. 17
    4
      include/Test.h
  2. 0
    39
      main_debug.cpp
  3. 78
    10
      src/Test.cpp

+ 17
- 4
include/Test.h View File

@@ -6,12 +6,25 @@
6 6
 class Test {
7 7
  public :
8 8
   Test( bool verbose );
9
-  static bool assertEqual( std::string result, std::string expectation );
10
-  static bool assertNotEqual( std::string result, std::string expectation );
9
+
10
+  bool print_assertEqual( std::string result, std::string expectation );
11
+  bool do_assertEqual( std::string result, std::string expectation );
12
+  bool assertEqual( std::string result, std::string expectation );
13
+
14
+  bool print_assertNotEqual( std::string result, std::string expectation );
15
+  bool do_assertNotEqual( std::string result, std::string expectation );
16
+  bool assertNotEqual( std::string result, std::string expectation );
17
+
18
+  bool print_assertInRange( int value, int min, int max );
19
+  bool do_assertInRange( int value, int min, int max );
20
+  bool assertInRange( int value, int min, int max );
21
+
22
+  bool print_assertNotInRange( int value, int min, int max );
23
+  bool do_assertNotInRange( int value, int min, int max );
24
+  bool assertNotInRange( int value, int min, int max );
11 25
 
12 26
  private :
13
-  bool verbose;
14
-  
27
+  bool verbose ;  
15 28
 };
16 29
 
17 30
 #endif

+ 0
- 39
main_debug.cpp View File

@@ -1,39 +0,0 @@
1
-#include <iostream>
2
-#include <string>
3
-#include "include/Crypt.h"
4
-#include "include/Test.h"
5
-
6
-char START = START;
7
-char END = END;
8
-
9
-/**
10
- @brief Take a string on command line argument to encrypt it. For vigenere encnyption, the text is encrypted with itself as a key
11
- **/
12
-int main( int argc, char* argv[]  )
13
-{
14
-    std::string txt = argv[1] ;
15
-
16
-  if( argc < 1 ){
17
-    std::cout << "Missing argument : need sentance to encrypt" << std::endl;
18
-    return 0;
19
-  }
20
-  if( argc > 1 ){
21
-    START = argv[2][0] ;
22
-  }
23
-  if( argc > 2 ){
24
-    END = argv[3][0] ;
25
-  }
26
-
27
-  std::string test_1 = "la mer à boire" ;
28
-  std::string test_2 = "xyzabc" ;
29
-  
30
-  Test::assertEqual( Crypt::cesar( test_1, 3, START, END ), "od phu à erluh") ;
31
-  Test::assertEqual( Crypt::vigenere( test_1, test_1, START, END ), "xb zjj à ddrjj") ;
32
-  Test::assertEqual( Crypt::atbash( test_1, START, END ), "oz nvi à ylriv") ;
33
-
34
-  Test::assertEqual( Crypt::cesar( test_2, 3, START, END ), "abcdef") ;
35
-  Test::assertEqual( Crypt::vigenere( test_2, test_1, START, END ), "jzzngu") ;
36
-  Test::assertEqual( Crypt::atbash( test_2, START, END ), "cbazyx") ;
37
-  
38
-  return 0;
39
-}

+ 78
- 10
src/Test.cpp View File

@@ -1,29 +1,97 @@
1 1
 #include "../include/Test.h"
2 2
 
3 3
 Test::Test( bool verbose = true ){
4
-  this->verbose = verbose;
4
+  this->verbose = verbose ;
5 5
 }
6 6
 
7
-bool Test::assertEqual( std::string result, std::string expected )
7
+bool Test::print_assertEqual( std::string result, std::string expected )
8 8
 {
9
-  if ( result == expected ){
10
-    std::cout << "[\033[32msuccess\033[0m] : result equals expectation : \033[36m" << result << "\033[0m" << std::endl;
9
+  if (Test::do_assertEqual( result, expected )){
10
+    std::cout << "[\033[32msuccess\033[0m] : result equals expectation : \033[36m" << result << "\033[0m" << std::endl ;
11 11
     return true;
12 12
   }else{
13
-    std::cout << "[\033[31mfailure\033[0m] : result \033[36m: " << result << "\033[0m" << std::endl
14
-	      << "            expectation :\033[36m" << expected << std::endl;
13
+    std::cout << "[\033[31mfailure\033[0m] : result \033[36m: " << result << "\033[0m" << " ≠ \033[36m" << expected << std::endl;
15 14
     return false;
16 15
   }
17 16
 }
18 17
 
19
-bool Test::assertNotEqual( std::string result, std::string expected )
18
+bool Test::do_assertEqual( std::string result, std::string expected )
19
+{
20
+  return ( result == expected );
21
+}
22
+
23
+bool Test::assertEqual( std::string result, std::string expected )
24
+{
25
+  return ( this->verbose )?
26
+    Test::print_assertEqual( result, expected ):
27
+    Test::do_assertEqual( result, expected );
28
+}
29
+
30
+bool Test::print_assertNotEqual( std::string result, std::string expected )
20 31
 {
21
-  if ( result == expected ){
32
+  if ( Test::do_assertNotEqual( result, expected ) ){
22 33
     std::cout << "[\033[31mfailure\033[0m] : result equals expectation : \033[36m" << result << "\033[0m" << std::endl;
34
+    return true;
35
+  }else{
36
+    std::cout << "[\033[32msuccess\033[0m] : \033[36m" << result << "\033[0m" << " ≠ \033[36m" << expected << "\033[0m" << std::endl;
23 37
     return false;
38
+  }
39
+}
40
+
41
+bool Test::do_assertNotEqual( std::string result, std::string expected )
42
+{
43
+  return !Test::assertEqual( result, expected );
44
+}
45
+
46
+bool Test::assertNotEqual( std::string result, std::string expected )
47
+{
48
+  return ( this->verbose )?
49
+    Test::print_assertNotEqual( result, expected ):
50
+    Test::do_assertNotEqual( result, expected );
51
+}
52
+
53
+bool Test::assertInRange( int value, int min, int max ){
54
+  return (this->verbose)?
55
+    Test::print_assertInRange( value, min, max ) :
56
+    Test::do_assertInRange( value, min, max ) ;
57
+}
58
+
59
+bool Test::do_assertInRange( int value, int min, int max ){
60
+  return ( value < min || value > max )?
61
+    false :
62
+    true ;
63
+}
64
+
65
+bool Test::print_assertInRange( int value, int min, int max ){
66
+  if( Test::do_assertInRange( value, min, max ) ){
67
+    std::cout << "[\033[32msuccess\033[0m] : \033[36m" << value << "\033[0m" << " in \033[36m[" << min << ";" << max << "]\033[0m" << std::endl ;
68
+    return true;
24 69
   }else{
25
-    std::cout << "[\033[32msuccess\033[0m] : result : \033[36m" << result << "\033[0m" << std::endl
26
-	      << "            expectation : \033[36m" << expected << "\033[0m" << std::endl;
70
+    std::cout << "[\033[31mfailure\033[0m] : \033[36m" << value << "\033[0m" << " not in \033[36m[" << min << ";" << max << "]\033[0m" << std::endl ;
71
+    return false;
72
+  }
73
+}
74
+
75
+bool Test::assertNotInRange( int value, int min, int max ){
76
+  return (this->verbose)?
77
+    Test::print_assertNotInRange( value, min, max ) :
78
+    Test::do_assertNotInRange( value, min, max ) ;
79
+}
80
+
81
+bool Test::do_assertNotInRange( int value, int min, int max ){
82
+  return ( value < min || value > max )?
83
+    true :
84
+    false ;
85
+}
86
+
87
+bool Test::print_assertNotInRange( int value, int min, int max ){
88
+  if( Test::do_assertNotInRange( value, min, max ) ){
89
+    std::cout << "[\033[32msuccess\033[0m] : value : \033[36m" << value << "\033[0m" << std::endl
90
+	      << "            range : \033[36m[" << min << ";" << max << "]\033[0m" << std::endl;
27 91
     return true;
92
+  }else{
93
+    std::cout << "[\033[31mfailure\033[0m] : value in range : \033[36m" << value << "\033[0m" << std::endl
94
+	      << "            range : \033[36m[" << min << ";" << max << "]\033[0m" << std::endl;
95
+    return false;
28 96
   }
29 97
 }

Loading…
Cancel
Save