TestCaseInfoHasher.tests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/catch_test_case_info.hpp>
  8. #include <catch2/internal/catch_test_case_info_hasher.hpp>
  9. static constexpr Catch::SourceLineInfo dummySourceLineInfo = CATCH_INTERNAL_LINEINFO;
  10. using Catch::TestCaseInfo;
  11. using Catch::TestCaseInfoHasher;
  12. TEST_CASE("Hashers with same seed produce same hash", "[test-case-hash]") {
  13. TestCaseInfo dummy( "", { "name", "[a-tag]" }, dummySourceLineInfo );
  14. TestCaseInfoHasher h1( 0x12345678 );
  15. TestCaseInfoHasher h2( 0x12345678 );
  16. REQUIRE( h1( dummy ) == h2( dummy ) );
  17. }
  18. TEST_CASE(
  19. "Hashers with different seed produce different hash with same test case",
  20. "[test-case-hash]") {
  21. TestCaseInfo dummy( "", { "name", "[a-tag]" }, dummySourceLineInfo );
  22. TestCaseInfoHasher h1( 0x12345678 );
  23. TestCaseInfoHasher h2( 0x87654321 );
  24. REQUIRE( h1( dummy ) != h2( dummy ) );
  25. }
  26. TEST_CASE("Hashing test case produces same hash across multiple calls",
  27. "[test-case-hash]") {
  28. TestCaseInfo dummy( "", { "name", "[a-tag]" }, dummySourceLineInfo );
  29. TestCaseInfoHasher h( 0x12345678 );
  30. REQUIRE( h( dummy ) == h( dummy ) );
  31. }
  32. TEST_CASE("Hashing different test cases produces different result", "[test-case-hash]") {
  33. TestCaseInfoHasher h( 0x12345678 );
  34. SECTION("Different test name") {
  35. TestCaseInfo dummy1( "class", { "name-1", "[a-tag]" }, dummySourceLineInfo );
  36. TestCaseInfo dummy2(
  37. "class", { "name-2", "[a-tag]" }, dummySourceLineInfo );
  38. REQUIRE( h( dummy1 ) != h( dummy2 ) );
  39. }
  40. SECTION("Different classname") {
  41. TestCaseInfo dummy1(
  42. "class-1", { "name", "[a-tag]" }, dummySourceLineInfo );
  43. TestCaseInfo dummy2(
  44. "class-2", { "name", "[a-tag]" }, dummySourceLineInfo );
  45. REQUIRE( h( dummy1 ) != h( dummy2 ) );
  46. }
  47. SECTION("Different tags") {
  48. TestCaseInfo dummy1(
  49. "class", { "name", "[a-tag]" }, dummySourceLineInfo );
  50. TestCaseInfo dummy2(
  51. "class", { "name", "[b-tag]" }, dummySourceLineInfo );
  52. REQUIRE( h( dummy1 ) != h( dummy2 ) );
  53. }
  54. }