• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2022 Red Hat, Inc.
5 
6 #include <iostream>
7 #include "test-utils.h"
8 
9 using std::string;
10 
11 namespace abigail
12 {
13 namespace tests
14 {
15 
16 /// Returns the absolute path to the source directory.
17 ///
18 /// \return the absolute path tho the source directory.
19 const char*
get_src_dir()20 get_src_dir()
21 {
22 #ifndef ABIGAIL_SRC_DIR
23 #error the macro ABIGAIL_SRC_DIR must be set at compile time
24 #endif
25 
26   static __thread const char* s(ABIGAIL_SRC_DIR);
27   return s;
28 }
29 
30 /// Returns the absolute path to the build directory.
31 ///
32 /// \return the absolute path the build directory.
33 const char*
get_build_dir()34 get_build_dir()
35 {
36 #ifndef ABIGAIL_BUILD_DIR
37 #error the macro ABIGAIL_BUILD_DIR must be set at compile time
38 #endif
39 
40   static __thread const char* s(ABIGAIL_BUILD_DIR);
41   return s;
42 }
43 
44 /// Emit test status on the standard output.
45 ///
46 /// This function also increments passed, failed and total test
47 /// numbers accordingly.
48 ///
49 /// @param test_passed indicated if the test succeeded or not.
50 ///
51 /// @param test_cmd the test command that was executed.  If the test
52 /// failed, the exact command is displayed.
53 ///
54 /// @param passed_count the number of passed tests.  This is going to
55 /// be incremented if the test passes.
56 ///
57 /// @param failed_count the number of failed tests.  This is going to
58 /// be incremented if the test fails.
59 ///
60 /// @param total_count the total number of tests.  This is going to be
61 /// incremented.
62 void
emit_test_status_and_update_counters(bool test_passed,const std::string & test_cmd,unsigned & passed_count,unsigned & failed_count,unsigned & total_count)63 emit_test_status_and_update_counters(bool test_passed,
64 				     const std::string& test_cmd,
65 				     unsigned& passed_count,
66 				     unsigned& failed_count,
67 				     unsigned& total_count)
68 {
69   if (test_passed)
70     passed_count++;
71   else
72     {
73       std::cout << TEST_FAILURE_COLOR
74 		<< "Test Failed: "
75 		<< DEFAULT_TERMINAL_COLOR
76 		<< test_cmd
77 		<< std::endl;
78       failed_count++;
79     }
80   total_count++;
81 }
82 
83 /// Emit the summary of the test.
84 ///
85 /// @param total_count the total number of tests executed.
86 ///
87 /// @param passed_count the number of tests that succeeded.
88 ///
89 /// @param failed_count the number of tests that failed.
90 void
emit_test_summary(unsigned total_count,unsigned passed_count,unsigned failed_count)91 emit_test_summary(unsigned total_count,
92 		  unsigned passed_count,
93 		  unsigned failed_count)
94 {
95   if (failed_count)
96     std::cout << TEST_FAILURE_COLOR << "FAILURE!";
97   else
98     std::cout << TEST_SUCCESS_COLOR << "SUCCESS!";
99   std::cout << DEFAULT_TERMINAL_COLOR << "\n";
100 
101   std::cout << "Total number of tests executed: " << total_count
102 	    << " Number of tests PASSED: " << passed_count
103 	    << ", Number of tests FAILED: " << failed_count
104 	    << ".\n";
105 }
106 }//end namespace tests
107 }//end namespace abigail
108