String routines to complement <string> && <cstring>:
uUnit.h
Go to the documentation of this file.
1 /* uUnit.h (C) 2011 adolfo@di-mare.com */
2 
3 /** \file uUnit.h
4  \brief [u]Micro module for [Unit] program testing.
5 
6  \author Adolfo Di Mare <adolfo@di-mare.com>
7  \date 2011,2019
8  \see http://www.di-mare.com/adolfo/p/BUnitXP.htm
9 
10 \code
11  #include "uUnit.h" // assertTrue() && assertFalse()
12 
13  // Example
14  int main( int argc , const char* argv[] ) {
15  std::cout << "TestCase [uUnit.h] yesYES" << std::endl;
16  assertTrue( 1==2 ); // ok?
17  assertFalse( 1==2 ); // ok!
18  return test_myImp(argc , argv);
19  }
20 \endcode
21 */
22 
23 #ifndef ADOLFO_UUNIT
24 #define ADOLFO_UUNIT
25 
26 /* #include "BUnit.h" // http://www.di-mare.com/adolfo/p/BUnit.htm */
27 /* #include "BUnit.h" // http://www.di-mare.com/adolfo/p/BUnitXP.htm */
28 
29 /* inline [C++] && static [C] trick
30  * -> TRICK to avoid using a uUnit.cpp implementation file
31  * -> TRICK to avoid "function already defined" linkage error */
32 
33 #ifdef __cplusplus
34  /** "C:/DIR/SubDir/file_name.ext" ==> "file_name.ext" */
35  template <class T>
36  const T* remove_dir_name( const T* str );
37  /** "C:/DIR/SubDir/file_name.ext" ==> "file_name.ext" */
38  template <>
39  const char* remove_dir_name<char>( const char* str )
40 #else
41  /** "C:/DIR/SubDir/file_name.ext" ==> "file_name.ext" */
42  static /* "static" avoids "function already defined" linkage error */
43  const char* remove_dir_name( const char* str )
44 
45  /* Only for C [not C++]
46  In C, each object file will contain a local compiled copy of this
47  function because it is declared 'static'.
48  - This duplication is the price paid to avoid using a uUnit.cpp file.
49  */
50 #endif /* __cplusplus */
51 {
52  const char *p = str; /* p = str+strlen(str); */
53  while ( *p != 0 ) { ++p; }
54  while ( *p != '/' && *p != '\\' && *p != ':' ) {
55  if ( p == str ) { return str; }
56  --p;
57  }
58  return p+1;
59 }
60 
61 #ifdef __cplusplus
62  #include <iostream> /* std::cout */
63  template <class T> inline /* inline avoids 'multiple definition' warning */
64  void uUnit_testThis( bool cond, const T* label, const T* fname, long lineno );
65  template <> inline
67  bool cond, const char* label, const char* fname, long lineno
68  ) {
69  if (!cond) {
70  std::cout << "=\\_fail: " << label << " \n=/ (";
71  std::cout << lineno << ") " << remove_dir_name(fname) << "\n";
72  }
73  }
74 #else
75  #include <stdio.h> /* printf() */
76  static
77  void uUnit_testThis(
78  int cond, const char* label, const char* fname, long lineno
79  ) {
80  if (!cond) {
81  printf( "%s%s%s", "=\\_fail: " , label , " \n=/ (" );
82  printf( "%ld%s%s%s", lineno , ") " , remove_dir_name(fname),"\n" );
83  }
84  }
85 #endif
86 
87 
88 #ifdef NDEBUG
89  #define assertTrue(x) ((void)0)
90  #define assertFalse(x) ((void)0)
91 #else /* debugging enabled */
92  /** (cond ? () : cout << "cond" ) */
93  #define assertTrue(cond) uUnit_testThis(!!(cond), #cond, __FILE__,__LINE__ )
94  /** (!(cond) ? () : cout << "!" << (cond)" ) */
95  #define assertFalse(cond) uUnit_testThis( !(cond),"!(" #cond ")", __FILE__,__LINE__ )
96 #endif
97 
98 #endif
99 
100 /* EOF: uUnit.h */
remove_dir_name
const T * remove_dir_name(const T *str)
"C:/DIR/SubDir/file_name.ext" ==> "file_name.ext"
uUnit_testThis< char >
void uUnit_testThis< char >(bool cond, const char *label, const char *fname, long lineno)
Definition: uUnit.h:66
remove_dir_name< char >
const char * remove_dir_name< char >(const char *str)
"C:/DIR/SubDir/file_name.ext" ==> "file_name.ext"
Definition: uUnit.h:39
uUnit_testThis
void uUnit_testThis(bool cond, const T *label, const T *fname, long lineno)