libslic3r_tests.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <catch_main.hpp>
  2. #include "libslic3r/Utils.hpp"
  3. // bimap test
  4. #include <string_view>
  5. #include <boost/bimap.hpp>
  6. #include <boost/assign.hpp>
  7. namespace {
  8. TEST_CASE("sort_remove_duplicates", "[utils]") {
  9. std::vector<int> data_src = { 3, 0, 2, 1, 15, 3, 5, 6, 3, 1, 0 };
  10. std::vector<int> data_dst = { 0, 1, 2, 3, 5, 6, 15 };
  11. Slic3r::sort_remove_duplicates(data_src);
  12. REQUIRE(data_src == data_dst);
  13. }
  14. TEST_CASE("string_printf", "[utils]") {
  15. SECTION("Empty format with empty data should return empty string") {
  16. std::string outs = Slic3r::string_printf("");
  17. REQUIRE(outs.empty());
  18. }
  19. SECTION("String output length should be the same as input") {
  20. std::string outs = Slic3r::string_printf("1234");
  21. REQUIRE(outs.size() == 4);
  22. }
  23. SECTION("String format should be interpreted as with sprintf") {
  24. std::string outs = Slic3r::string_printf("%d %f %s", 10, 11.4, " This is a string");
  25. char buffer[1024];
  26. sprintf(buffer, "%d %f %s", 10, 11.4, " This is a string");
  27. REQUIRE(outs.compare(buffer) == 0);
  28. }
  29. SECTION("String format should survive large input data") {
  30. std::string input(2048, 'A');
  31. std::string outs = Slic3r::string_printf("%s", input.c_str());
  32. REQUIRE(outs.compare(input) == 0);
  33. }
  34. }
  35. TEST_CASE("Bimap duplicity behavior") {
  36. enum class number {
  37. one = 1,
  38. three = 3,
  39. tri = 3 // ONLY alias
  40. };
  41. using BimapType = boost::bimap<std::string_view, number>;
  42. BimapType bimap = boost::assign::list_of<BimapType::relation>
  43. ("one", number::one)
  44. ("three", number::three)
  45. ("tri", number::tri) // no matter if it is there
  46. ;
  47. const auto& to_type = bimap.left;
  48. auto item_number1 = to_type.find("one");
  49. REQUIRE(item_number1 != to_type.end());
  50. CHECK(item_number1->second == number::one);
  51. auto item_number3 = to_type.find("three");
  52. REQUIRE(item_number3 != to_type.end());
  53. CHECK(item_number3->second == number::three);
  54. // to_type.find("tri"); // not in map
  55. const auto &to_name = bimap.right;
  56. auto it1 = to_name.find(number::one);
  57. REQUIRE(it1 != to_name.end());
  58. CHECK(it1->second == "one");
  59. auto it2 = to_name.find(number::three);
  60. REQUIRE(it2 != to_name.end());
  61. CHECK(it2->second == "three");
  62. auto it3 = to_name.find(number::tri);
  63. REQUIRE(it3 != to_name.end());
  64. REQUIRE(number::three == number::tri);
  65. CHECK(it3->second == "three");
  66. }
  67. } // end namespace