test_config.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Config.hpp"
  3. #include "libslic3r/PrintConfig.hpp"
  4. #include "libslic3r/LocalesUtils.hpp"
  5. #include <cereal/types/polymorphic.hpp>
  6. #include <cereal/types/string.hpp>
  7. #include <cereal/types/vector.hpp>
  8. #include <cereal/archives/binary.hpp>
  9. using namespace Slic3r;
  10. TEST_CASE("Dynamic config serialization - tests ConfigBase", "[Config]"){
  11. DynamicPrintConfig config;
  12. INFO("Serialize float");
  13. config.set_key_value("layer_height", new ConfigOptionFloat(0.3));
  14. CHECK(config.opt_serialize("layer_height") == "0.3");
  15. INFO("Serialize int");
  16. config.set_key_value("perimeters", new ConfigOptionInt(2));
  17. CHECK(config.opt_serialize("perimeters") == "2");
  18. INFO("Serialize float or percent");
  19. config.set_key_value("first_layer_height", new ConfigOptionFloatOrPercent(30, true));
  20. CHECK(config.opt_serialize("first_layer_height") == "30%");
  21. INFO("Serialize bool");
  22. config.set_key_value("use_relative_e_distances", new ConfigOptionBool(true));
  23. CHECK(config.opt_serialize("use_relative_e_distances") == "1");
  24. INFO("Serialize enum");
  25. config.set_key_value("gcode_flavor", new ConfigOptionEnum<GCodeFlavor>(gcfTeacup));
  26. CHECK(config.opt_serialize("gcode_flavor") == "teacup");
  27. INFO("Serialize string");
  28. config.set_key_value("extrusion_axis", new ConfigOptionString("A"));
  29. CHECK(config.opt_serialize("extrusion_axis") == "A");
  30. INFO("Serialize string with newline");
  31. config.set_key_value("notes", new ConfigOptionString("foo\nbar"));
  32. CHECK(config.opt_serialize("notes") == "foo\\nbar");
  33. config.set_deserialize_strict("notes", "bar\\nbaz");
  34. INFO("Deserialize string with newline");
  35. CHECK(config.opt_string("notes") == "bar\nbaz");
  36. INFO("Serialize points");
  37. config.set_key_value("extruder_offset", new ConfigOptionPoints({{10, 20}, {30, 45}}));
  38. CHECK(config.opt_serialize("extruder_offset") == "10x20,30x45");
  39. INFO("Deserialize points");
  40. config.set_deserialize_strict("extruder_offset", "20x10");
  41. CHECK(config.option<ConfigOptionPoints>("extruder_offset")->values == std::vector{Vec2d{20, 10}});
  42. INFO("Serialize floats");
  43. config.set_key_value("nozzle_diameter", new ConfigOptionFloats({0.2, 3}));
  44. CHECK(config.opt_serialize("nozzle_diameter") == "0.2,3");
  45. INFO("Deserialize floats");
  46. config.set_deserialize_strict("nozzle_diameter", "0.1,0.4");
  47. CHECK_THAT(config.option<ConfigOptionFloats>("nozzle_diameter")->values, Catch::Matchers::Approx(std::vector{0.1, 0.4}));
  48. INFO("Deserialize floats from one value");
  49. config.set_deserialize_strict("nozzle_diameter", "3");
  50. CHECK_THAT(config.option<ConfigOptionFloats>("nozzle_diameter")->values, Catch::Matchers::Approx(std::vector{3.0}));
  51. INFO("Serialize ints");
  52. config.set_key_value("temperature", new ConfigOptionInts({180, 210}));
  53. CHECK(config.opt_serialize("temperature") == "180,210");
  54. INFO("Deserialize ints");
  55. config.set_deserialize_strict("temperature", "195,220");
  56. CHECK(config.option<ConfigOptionInts>("temperature")->values == std::vector{195,220});
  57. INFO("Serialize bools");
  58. config.set_key_value("wipe", new ConfigOptionBools({true, false}));
  59. CHECK(config.opt_serialize("wipe") == "1,0");
  60. INFO("Deserialize bools");
  61. config.set_deserialize_strict("wipe", "0,1,1");
  62. CHECK(config.option<ConfigOptionBools>("wipe")->values == std::vector<unsigned char>{false, true, true});
  63. INFO("Deserialize bools from empty stirng");
  64. config.set_deserialize_strict("wipe", "");
  65. CHECK(config.option<ConfigOptionBools>("wipe")->values == std::vector<unsigned char>{});
  66. INFO("Deserialize bools from value");
  67. config.set_deserialize_strict({{"wipe", 1}});
  68. CHECK(config.option<ConfigOptionBools>("wipe")->values == std::vector<unsigned char>{true});
  69. INFO("Serialize strings");
  70. config.set_key_value("post_process", new ConfigOptionStrings({"foo", "bar"}));
  71. CHECK(config.opt_serialize("post_process") == "foo;bar");
  72. INFO("Deserialize strings");
  73. config.set_deserialize_strict("post_process", "bar;baz");
  74. CHECK(config.option<ConfigOptionStrings>("post_process")->values == std::vector<std::string>{"bar", "baz"});
  75. }
  76. TEST_CASE("Get keys", "[Config]"){
  77. DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
  78. CHECK(!config.keys().empty());
  79. }
  80. TEST_CASE("Set not already set option", "[Config]") {
  81. DynamicPrintConfig config;
  82. config.set_deserialize_strict("filament_diameter", "3");
  83. }
  84. TEST_CASE("Config apply dynamic to static", "[Config]") {
  85. DynamicPrintConfig config;
  86. config.set_deserialize_strict("perimeters", "2");
  87. // This trick is taken directly from perl.
  88. StaticPrintConfig* config2 = static_cast<GCodeConfig*>(new FullPrintConfig());
  89. config2->apply(config, true);
  90. CHECK(config2->opt_int("perimeters") == 2);
  91. delete config2;
  92. }
  93. TEST_CASE("Config apply static to dynamic", "[Config]") {
  94. // This trick is taken directly from perl.
  95. StaticPrintConfig* config = static_cast<GCodeConfig*>(new FullPrintConfig());
  96. DynamicPrintConfig config2;
  97. config2.apply(*config, true);
  98. delete config;
  99. CHECK(
  100. config2.opt_int("perimeters") ==
  101. DynamicPrintConfig::full_print_config().opt_int("perimeters")
  102. );
  103. }
  104. TEST_CASE("Config apply dynamic to dynamic", "[Config]") {
  105. DynamicPrintConfig config;
  106. config.set_key_value("extruder_offset", new ConfigOptionPoints({{0, 0}, {20, 0}, {0, 20}}));
  107. DynamicPrintConfig config2;
  108. config2.apply(config, true);
  109. CHECK(
  110. config2.option<ConfigOptionPoints>("extruder_offset")->values ==
  111. std::vector<Vec2d>{{0, 0}, {20, 0}, {0, 20}}
  112. );
  113. }
  114. TEST_CASE("Get abs value on percent", "[Config]") {
  115. StaticPrintConfig* config = static_cast<GCodeConfig*>(new FullPrintConfig());
  116. config->set_deserialize_strict("solid_infill_speed", "60");
  117. config->set_deserialize_strict("top_solid_infill_speed", "10%");
  118. CHECK(config->get_abs_value("top_solid_infill_speed") == 6);
  119. delete config;
  120. }
  121. TEST_CASE("No interference between DynamicConfig objects", "[Config]") {
  122. DynamicPrintConfig config;
  123. config.set_key_value("fill_pattern", new ConfigOptionString("line"));
  124. DynamicPrintConfig config2;
  125. config2.set_key_value("fill_pattern", new ConfigOptionString("hilbertcurve"));
  126. CHECK(config.opt_string("fill_pattern") == "line");
  127. }
  128. TEST_CASE("Normalize fdm extruder", "[Config]") {
  129. DynamicPrintConfig config;
  130. config.set("extruder", 2, true);
  131. config.set("perimeter_extruder", 3, true);
  132. config.normalize_fdm();
  133. INFO("Extruder option is removed after normalize().");
  134. CHECK(!config.has("extruder"));
  135. INFO("Undefined extruder is populated with default extruder.");
  136. CHECK(config.opt_int("infill_extruder") == 2);
  137. INFO("Defined extruder is not overwritten by default extruder.");
  138. CHECK(config.opt_int("perimeter_extruder") == 3);
  139. }
  140. TEST_CASE("Normalize fdm infill extruder", "[Config]") {
  141. DynamicPrintConfig config;
  142. config.set("infill_extruder", 2, true);
  143. config.normalize_fdm();
  144. INFO("Undefined solid infill extruder is populated with infill extruder.");
  145. CHECK(config.opt_int("solid_infill_extruder") == 2);
  146. }
  147. TEST_CASE("Normalize fdm retract layer change", "[Config]") {
  148. DynamicPrintConfig config;
  149. config.set("spiral_vase", true, true);
  150. config.set_key_value("retract_layer_change", new ConfigOptionBools({true, false}));
  151. config.normalize_fdm();
  152. CHECK(config.option<ConfigOptionBools>("retract_layer_change")->values == std::vector<unsigned char>{0, 0});
  153. }
  154. TEST_CASE("Can read ini with invalid items", "[Config]") {
  155. std::string path = std::string(TEST_DATA_DIR) + "/test_config/bad_config_options.ini";
  156. DynamicPrintConfig config;
  157. config.load(path, ForwardCompatibilitySubstitutionRule::Disable);
  158. //Did not crash.
  159. }
  160. struct SerializationTestData {
  161. std::string name;
  162. std::vector<std::string> values;
  163. std::string serialized;
  164. };
  165. TEST_CASE("Config serialization of multiple values", "[Config]"){
  166. DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
  167. std::vector<SerializationTestData> test_data{
  168. {
  169. "empty",
  170. {},
  171. ""
  172. },
  173. {
  174. "single empty",
  175. {""},
  176. "\"\""
  177. },
  178. {
  179. "single noempty, simple",
  180. {"RGB"},
  181. "RGB"
  182. },
  183. {
  184. "multiple noempty, simple",
  185. {"ABC", "DEF", "09182745@!#$*(&"},
  186. "ABC;DEF;09182745@!#$*(&"
  187. },
  188. {
  189. "multiple, simple, some empty",
  190. {"ABC", "DEF", "", "09182745@!#$*(&", ""},
  191. "ABC;DEF;;09182745@!#$*(&;"
  192. },
  193. {
  194. "complex",
  195. {"some \"quoted\" notes", "yet\n some notes", "whatever \n notes", ""},
  196. "\"some \\\"quoted\\\" notes\";\"yet\\n some notes\";\"whatever \\n notes\";"
  197. }
  198. };
  199. for (const SerializationTestData& data : test_data) {
  200. config.set_key_value("filament_notes", new ConfigOptionStrings(data.values));
  201. CHECK(config.opt_serialize("filament_notes") == data.serialized);
  202. config.set_deserialize_strict("filament_notes", "");
  203. CHECK(config.option<ConfigOptionStrings>("filament_notes")->values == std::vector<std::string>{});
  204. config.set_deserialize_strict("filament_notes", data.serialized);
  205. CHECK(config.option<ConfigOptionStrings>("filament_notes")->values == data.values);
  206. }
  207. }
  208. SCENARIO("Generic config validation performs as expected.", "[Config]") {
  209. GIVEN("A config generated from default options") {
  210. Slic3r::DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  211. WHEN("perimeter_extrusion_width is set to 250%, a valid value") {
  212. config.set_deserialize_strict("perimeter_extrusion_width", "250%");
  213. THEN( "The config is read as valid.") {
  214. REQUIRE(config.validate().empty());
  215. }
  216. }
  217. WHEN("perimeter_extrusion_width is set to -10, an invalid value") {
  218. config.set("perimeter_extrusion_width", -10);
  219. THEN( "Validate returns error") {
  220. REQUIRE(! config.validate().empty());
  221. }
  222. }
  223. WHEN("perimeters is set to -10, an invalid value") {
  224. config.set("perimeters", -10);
  225. THEN( "Validate returns error") {
  226. REQUIRE(! config.validate().empty());
  227. }
  228. }
  229. }
  230. }
  231. SCENARIO("Config accessor functions perform as expected.", "[Config]") {
  232. auto test = [](ConfigBase &config) {
  233. WHEN("A boolean option is set to a boolean value") {
  234. REQUIRE_NOTHROW(config.set("gcode_comments", true));
  235. THEN("The underlying value is set correctly.") {
  236. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == true);
  237. }
  238. }
  239. WHEN("A boolean option is set to a string value representing a 0 or 1") {
  240. CHECK_NOTHROW(config.set_deserialize_strict("gcode_comments", "1"));
  241. THEN("The underlying value is set correctly.") {
  242. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == true);
  243. }
  244. }
  245. WHEN("A boolean option is set to a string value representing something other than 0 or 1") {
  246. THEN("A BadOptionTypeException exception is thrown.") {
  247. REQUIRE_THROWS_AS(config.set("gcode_comments", "Z"), BadOptionTypeException);
  248. }
  249. AND_THEN("Value is unchanged.") {
  250. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == false);
  251. }
  252. }
  253. WHEN("A boolean option is set to an int value") {
  254. THEN("A BadOptionTypeException exception is thrown.") {
  255. REQUIRE_THROWS_AS(config.set("gcode_comments", 1), BadOptionTypeException);
  256. }
  257. }
  258. WHEN("A numeric option is set from serialized string") {
  259. config.set_deserialize_strict("bed_temperature", "100");
  260. THEN("The underlying value is set correctly.") {
  261. REQUIRE(config.opt<ConfigOptionInts>("bed_temperature")->get_at(0) == 100);
  262. }
  263. }
  264. #if 0
  265. //FIXME better design accessors for vector elements.
  266. WHEN("An integer-based option is set through the integer interface") {
  267. config.set("bed_temperature", 100);
  268. THEN("The underlying value is set correctly.") {
  269. REQUIRE(config.opt<ConfigOptionInts>("bed_temperature")->get_at(0) == 100);
  270. }
  271. }
  272. #endif
  273. WHEN("An floating-point option is set through the integer interface") {
  274. config.set("perimeter_speed", 10);
  275. THEN("The underlying value is set correctly.") {
  276. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 10.0);
  277. }
  278. }
  279. WHEN("A floating-point option is set through the double interface") {
  280. config.set("perimeter_speed", 5.5);
  281. THEN("The underlying value is set correctly.") {
  282. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 5.5);
  283. }
  284. }
  285. WHEN("An integer-based option is set through the double interface") {
  286. THEN("A BadOptionTypeException exception is thrown.") {
  287. REQUIRE_THROWS_AS(config.set("bed_temperature", 5.5), BadOptionTypeException);
  288. }
  289. }
  290. WHEN("A numeric option is set to a non-numeric value.") {
  291. THEN("A BadOptionTypeException exception is thown.") {
  292. REQUIRE_THROWS_AS(config.set_deserialize_strict("perimeter_speed", "zzzz"), BadOptionValueException);
  293. }
  294. THEN("The value does not change.") {
  295. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 60.0);
  296. }
  297. }
  298. WHEN("A string option is set through the string interface") {
  299. config.set("end_gcode", "100");
  300. THEN("The underlying value is set correctly.") {
  301. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == "100");
  302. }
  303. }
  304. WHEN("A string option is set through the integer interface") {
  305. config.set("end_gcode", 100);
  306. THEN("The underlying value is set correctly.") {
  307. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == "100");
  308. }
  309. }
  310. WHEN("A string option is set through the double interface") {
  311. config.set("end_gcode", 100.5);
  312. THEN("The underlying value is set correctly.") {
  313. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == float_to_string_decimal_point(100.5));
  314. }
  315. }
  316. WHEN("A float or percent is set as a percent through the string interface.") {
  317. config.set_deserialize_strict("first_layer_extrusion_width", "100%");
  318. THEN("Value and percent flag are 100/true") {
  319. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  320. REQUIRE(tmp->percent == true);
  321. REQUIRE(tmp->value == 100);
  322. }
  323. }
  324. WHEN("A float or percent is set as a float through the string interface.") {
  325. config.set_deserialize_strict("first_layer_extrusion_width", "100");
  326. THEN("Value and percent flag are 100/false") {
  327. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  328. REQUIRE(tmp->percent == false);
  329. REQUIRE(tmp->value == 100);
  330. }
  331. }
  332. WHEN("A float or percent is set as a float through the int interface.") {
  333. config.set("first_layer_extrusion_width", 100);
  334. THEN("Value and percent flag are 100/false") {
  335. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  336. REQUIRE(tmp->percent == false);
  337. REQUIRE(tmp->value == 100);
  338. }
  339. }
  340. WHEN("A float or percent is set as a float through the double interface.") {
  341. config.set("first_layer_extrusion_width", 100.5);
  342. THEN("Value and percent flag are 100.5/false") {
  343. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  344. REQUIRE(tmp->percent == false);
  345. REQUIRE(tmp->value == 100.5);
  346. }
  347. }
  348. WHEN("An invalid option is requested during set.") {
  349. THEN("A BadOptionTypeException exception is thrown.") {
  350. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", 1), UnknownOptionException);
  351. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", 1.0), UnknownOptionException);
  352. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", "1"), UnknownOptionException);
  353. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", true), UnknownOptionException);
  354. }
  355. }
  356. WHEN("An invalid option is requested during get.") {
  357. THEN("A UnknownOptionException exception is thrown.") {
  358. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionString>("deadbeef_invalid_option", false), UnknownOptionException);
  359. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionFloat>("deadbeef_invalid_option", false), UnknownOptionException);
  360. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionInt>("deadbeef_invalid_option", false), UnknownOptionException);
  361. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionBool>("deadbeef_invalid_option", false), UnknownOptionException);
  362. }
  363. }
  364. WHEN("An invalid option is requested during opt.") {
  365. THEN("A UnknownOptionException exception is thrown.") {
  366. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionString>("deadbeef_invalid_option", false), UnknownOptionException);
  367. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionFloat>("deadbeef_invalid_option", false), UnknownOptionException);
  368. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionInt>("deadbeef_invalid_option", false), UnknownOptionException);
  369. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionBool>("deadbeef_invalid_option", false), UnknownOptionException);
  370. }
  371. }
  372. WHEN("getX called on an unset option.") {
  373. THEN("The default is returned.") {
  374. REQUIRE(config.opt_float("layer_height") == 0.3);
  375. REQUIRE(config.opt_int("raft_layers") == 0);
  376. REQUIRE(config.opt_bool("support_material") == false);
  377. }
  378. }
  379. WHEN("getFloat called on an option that has been set.") {
  380. config.set("layer_height", 0.5);
  381. THEN("The set value is returned.") {
  382. REQUIRE(config.opt_float("layer_height") == 0.5);
  383. }
  384. }
  385. };
  386. GIVEN("DynamicPrintConfig generated from default options") {
  387. auto config = Slic3r::DynamicPrintConfig::full_print_config();
  388. test(config);
  389. }
  390. GIVEN("FullPrintConfig generated from default options") {
  391. Slic3r::FullPrintConfig config;
  392. test(config);
  393. }
  394. }
  395. SCENARIO("Config ini load/save interface", "[Config]") {
  396. WHEN("new_from_ini is called") {
  397. Slic3r::DynamicPrintConfig config;
  398. std::string path = std::string(TEST_DATA_DIR) + "/test_config/new_from_ini.ini";
  399. config.load_from_ini(path, ForwardCompatibilitySubstitutionRule::Disable);
  400. THEN("Config object contains ini file options.") {
  401. REQUIRE(config.option_throw<ConfigOptionStrings>("filament_colour", false)->values.size() == 1);
  402. REQUIRE(config.option_throw<ConfigOptionStrings>("filament_colour", false)->values.front() == "#ABCD");
  403. }
  404. }
  405. }
  406. SCENARIO("DynamicPrintConfig serialization", "[Config]") {
  407. WHEN("DynamicPrintConfig is serialized and deserialized") {
  408. FullPrintConfig full_print_config;
  409. DynamicPrintConfig cfg;
  410. cfg.apply(full_print_config, false);
  411. std::string serialized;
  412. try {
  413. std::ostringstream ss;
  414. cereal::BinaryOutputArchive oarchive(ss);
  415. oarchive(cfg);
  416. serialized = ss.str();
  417. } catch (const std::runtime_error & /* e */) {
  418. // e.what();
  419. }
  420. THEN("Config object contains ini file options.") {
  421. DynamicPrintConfig cfg2;
  422. try {
  423. std::stringstream ss(serialized);
  424. cereal::BinaryInputArchive iarchive(ss);
  425. iarchive(cfg2);
  426. } catch (const std::runtime_error & /* e */) {
  427. // e.what();
  428. }
  429. REQUIRE(cfg == cfg2);
  430. }
  431. }
  432. }