test_generators.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // (C) Copyright Andy Tompkins 2009. Permission to copy, use, modify, sell and
  2. // distribute this software is granted provided this copyright notice appears
  3. // in all copies. This software is provided "as is" without express or implied
  4. // warranty, and with no claim as to its suitability for any purpose.
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // https://www.boost.org/LICENSE_1_0.txt)
  8. // libs/uuid/test/test_generators.cpp -------------------------------//
  9. #include <boost/uuid/uuid.hpp>
  10. #include <boost/uuid/uuid_generators.hpp>
  11. #include <boost/uuid/uuid_io.hpp>
  12. #include <boost/test/included/test_exec_monitor.hpp>
  13. #include <boost/test/test_tools.hpp>
  14. #include <boost/random.hpp>
  15. template <typename RandomUuidGenerator>
  16. void check_random_generator(RandomUuidGenerator& uuid_gen)
  17. {
  18. boost::uuids::uuid u1 = uuid_gen();
  19. boost::uuids::uuid u2 = uuid_gen();
  20. BOOST_CHECK_NE(u1, u2);
  21. // check variant
  22. BOOST_CHECK_EQUAL(u1.variant(), boost::uuids::uuid::variant_rfc_4122);
  23. // BOOST_CHECK_EQUAL( *(u1.begin()+8) & 0xC0, 0x80);
  24. // version
  25. BOOST_CHECK_EQUAL( *(u1.begin()+6) & 0xF0, 0x40);
  26. }
  27. int test_main(int, char*[])
  28. {
  29. using namespace boost::uuids;
  30. using boost::test_tools::output_test_stream;
  31. { // test nil generator
  32. uuid u1 = nil_generator()();
  33. uuid u2 = {{0}};
  34. BOOST_CHECK_EQUAL(u1, u2);
  35. uuid u3 = nil_uuid();
  36. BOOST_CHECK_EQUAL(u3, u2);
  37. }
  38. { // test string_generator
  39. string_generator gen;
  40. uuid u = gen("00000000-0000-0000-0000-000000000000");
  41. BOOST_CHECK_EQUAL(u, nil_uuid());
  42. BOOST_CHECK_EQUAL(u.is_nil(), true);
  43. const uuid u_increasing = {{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }};
  44. const uuid u_decreasing = {{ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }};
  45. u = gen("0123456789abcdef0123456789ABCDEF");
  46. BOOST_CHECK_EQUAL(u, u_increasing);
  47. u = gen("{0123456789abcdef0123456789ABCDEF}");
  48. BOOST_CHECK_EQUAL(u, u_increasing);
  49. u = gen("{01234567-89AB-CDEF-0123-456789abcdef}");
  50. BOOST_CHECK_EQUAL(u, u_increasing);
  51. u = gen("01234567-89AB-CDEF-0123-456789abcdef");
  52. BOOST_CHECK_EQUAL(u, u_increasing);
  53. u = gen(std::string("fedcba98-7654-3210-fedc-ba9876543210"));
  54. BOOST_CHECK_EQUAL(u, u_decreasing);
  55. #ifndef BOOST_NO_STD_WSTRING
  56. u = gen(L"fedcba98-7654-3210-fedc-ba9876543210");
  57. BOOST_CHECK_EQUAL(u, u_decreasing);
  58. u = gen(std::wstring(L"01234567-89ab-cdef-0123-456789abcdef"));
  59. BOOST_CHECK_EQUAL(u, u_increasing);
  60. #endif //BOOST_NO_STD_WSTRING
  61. }
  62. { // test name_generator
  63. uuid dns_namespace_uuid = {{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
  64. uuid correct = {{0x21, 0xf7, 0xf8, 0xde, 0x80, 0x51, 0x5b, 0x89, 0x86, 0x80, 0x01, 0x95, 0xef, 0x79, 0x8b, 0x6a}};
  65. uuid wcorrect = {{0xb9, 0x50, 0x66, 0x13, 0x2c, 0x04, 0x51, 0x2d, 0xb8, 0xfe, 0xbf, 0x8d, 0x0b, 0xa1, 0xb2, 0x71}};
  66. name_generator gen(dns_namespace_uuid);
  67. uuid u = gen("www.widgets.com");
  68. BOOST_CHECK_EQUAL(u, correct);
  69. BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
  70. u = gen(L"www.widgets.com");
  71. BOOST_CHECK_EQUAL(u, wcorrect);
  72. BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
  73. u = gen(std::string("www.widgets.com"));
  74. BOOST_CHECK_EQUAL(u, correct);
  75. BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
  76. u = gen(std::wstring(L"www.widgets.com"));
  77. BOOST_CHECK_EQUAL(u, wcorrect);
  78. BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
  79. char name[] = "www.widgets.com";
  80. u = gen(name, 15);
  81. BOOST_CHECK_EQUAL(u, correct);
  82. BOOST_CHECK_EQUAL(u.variant(), boost::uuids::uuid::variant_rfc_4122);
  83. }
  84. { // test random_generator
  85. // default random number generator
  86. random_generator uuid_gen1;
  87. check_random_generator(uuid_gen1);
  88. // specific random number generator
  89. basic_random_generator<boost::rand48> uuid_gen2;
  90. check_random_generator(uuid_gen2);
  91. // pass by reference
  92. boost::ecuyer1988 ecuyer1988_gen;
  93. basic_random_generator<boost::ecuyer1988> uuid_gen3(ecuyer1988_gen);
  94. check_random_generator(uuid_gen3);
  95. // pass by pointer
  96. boost::lagged_fibonacci607 lagged_fibonacci607_gen;
  97. basic_random_generator<boost::lagged_fibonacci607> uuid_gen4(&lagged_fibonacci607_gen);
  98. check_random_generator(uuid_gen4);
  99. // random device
  100. //basic_random_generator<boost::random_device> uuid_gen5;
  101. //check_random_generator(uuid_gen5);
  102. }
  103. return 0;
  104. }