test_config.cpp 21 KB

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