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