You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.0 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/component/test.component.inserters.threadId/test.component.inserters.threadId.cpp
  3. *
  4. * Purpose: Implementation file for the test.component.inserters.threadId project.
  5. *
  6. * Created: 17th October 2006
  7. * Updated: 6th August 2012
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2006-2012, Synesis Software Pty Ltd.
  14. * All rights reserved.
  15. *
  16. * www: http://www.synesis.com.au/software
  17. *
  18. * ////////////////////////////////////////////////////////////////////// */
  19. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  20. /* xTests Header Files */
  21. #include <xtests/xtests.h>
  22. /* Pantheios Header Files */
  23. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  24. #include <pantheios/inserters/threadid.hpp> // for pantheios::threadId
  25. #include <pantheios/backends/bec.test.h>
  26. #include <pantheios/internal/threading.h>
  27. /* STLSoft Header Files */
  28. #include <stlsoft/conversion/integer_to_string.hpp>
  29. #include <platformstl/platformstl.h>
  30. /* Standard C Header Files */
  31. #include <stdlib.h> // for exit codes
  32. #if defined(PLATFORMSTL_OS_IS_UNIX)
  33. # include <unistd.h>
  34. #endif /* OS */
  35. #ifdef PANTHEIOS_MT
  36. # if defined(PLATFORMSTL_OS_IS_UNIX)
  37. # include <pthread.h>
  38. # elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  39. # include <windows.h>
  40. # else /* ? OS */
  41. # error Not discriminated for platforms other than UNIX and Windows
  42. # endif /* OS */
  43. #endif /* PANTHEIOS_MT */
  44. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  45. /* /////////////////////////////////////////////////////////////////////////
  46. * Forward declarations
  47. */
  48. static void test_1_01();
  49. static pantheios::sint64_t pan_get_tid_();
  50. /* ////////////////////////////////////////////////////////////////////// */
  51. /* Define the stock front-end process identity, so that it links when using
  52. * fe.N, fe.simple, etc. */
  53. PANTHEIOS_EXTERN_C PAN_CHAR_T const PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.component.inserters.threadId");
  54. /* ////////////////////////////////////////////////////////////////////// */
  55. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Character encoding
  58. */
  59. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  60. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_WIDE_STRING_EQUAL
  61. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  62. # define XTESTS_TEST_STRING_EQUAL XTESTS_TEST_MULTIBYTE_STRING_EQUAL
  63. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  64. /* ////////////////////////////////////////////////////////////////////// */
  65. int main(int argc, char** argv)
  66. {
  67. int retCode = EXIT_SUCCESS;
  68. int verbosity = 2;
  69. XTESTS_COMMANDLINE_PARSEVERBOSITY(argc, argv, &verbosity);
  70. if(XTESTS_START_RUNNER("test.component.inserters.threadId", verbosity))
  71. {
  72. XTESTS_RUN_CASE(test_1_01);
  73. XTESTS_PRINT_RESULTS();
  74. XTESTS_END_RUNNER_UPDATE_EXITCODE(&retCode);
  75. }
  76. return retCode;
  77. }
  78. /* ////////////////////////////////////////////////////////////////////// */
  79. static void test_1_01()
  80. {
  81. // 1. Setup
  82. const PAN_CHAR_T prefix[] = PSTR("thread: ");
  83. PAN_CHAR_T tid_[21 + STLSOFT_NUM_ELEMENTS(prefix)];
  84. PAN_CHAR_T const* tid = stlsoft::integer_to_string(&tid_[0], STLSOFT_NUM_ELEMENTS(tid_), pan_get_tid_());
  85. PAN_CHAR_T const* stmt = tid - (STLSOFT_NUM_ELEMENTS(prefix) - 1);
  86. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  87. ::wcsncpy(const_cast<PAN_CHAR_T*>(stmt), prefix, (STLSOFT_NUM_ELEMENTS(prefix) - 1));
  88. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  89. ::strncpy(const_cast<PAN_CHAR_T*>(stmt), prefix, (STLSOFT_NUM_ELEMENTS(prefix) - 1));
  90. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  91. pantheios::be::test::reset();
  92. // 2. Create test data
  93. pantheios::log_NOTICE(pantheios::threadId);
  94. pantheios::log_NOTICE(prefix, pantheios::threadId);
  95. // 3. Verification
  96. pantheios::be::test::Results results = pantheios::be::test::results();
  97. XTESTS_TEST(!results.empty());
  98. XTESTS_TEST(2 == results.size());
  99. XTESTS_TEST_STRING_EQUAL(tid, results[0].statement);
  100. XTESTS_TEST_STRING_EQUAL(stmt, results[1].statement);
  101. }
  102. static pantheios::sint64_t pan_get_tid_()
  103. {
  104. #if defined(PLATFORMSTL_OS_IS_UNIX)
  105. # ifdef PANTHEIOS_MT
  106. union
  107. {
  108. pantheios::sint64_t u64;
  109. pthread_t self;
  110. } u;
  111. STLSOFT_STATIC_ASSERT(sizeof(::pthread_self()) <= sizeof(pantheios::sint64_t));
  112. u.u64 = 0;
  113. u.self = ::pthread_self();
  114. return u.u64;
  115. # else /* ? PANTHEIOS_MT */
  116. return 1;
  117. # endif /* PANTHEIOS_MT */
  118. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  119. return static_cast<pantheios::sint64_t>(::GetCurrentThreadId());
  120. #else /* ? OS */
  121. # error Not discriminated for platforms other than UNIX and Windows
  122. #endif /* OS */
  123. }
  124. /* ///////////////////////////// end of file //////////////////////////// */