From 0ca56ed853c3d9bc8c26d1b014d8b665363fc2d0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 15 Sep 2016 01:01:58 -0700 Subject: 3355 --- html/071recipe.cc.html | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'html/071recipe.cc.html') diff --git a/html/071recipe.cc.html b/html/071recipe.cc.html index 47204f71..56f55901 100644 --- a/html/071recipe.cc.html +++ b/html/071recipe.cc.html @@ -212,22 +212,29 @@ Transform.push_back(} recipe from_reagent(const reagent& r) { - assert(!r.type->atom && r.type->left->atom && r.type->left->name == "recipe"); + assert(r.type); recipe result_header; // will contain only ingredients and products, nothing else result_header.has_header = true; + if (r.type->atom) { + assert(r.type->name == "recipe"); + return result_header; + } + assert(root_type(r.type)->name == "recipe"); const type_tree* curr = r.type->right; for (/*nada*/; curr && !curr->atom; curr = curr->right) { if (curr->left->atom && curr->left->name == "->") { curr = curr->right; // skip delimiter - break; + goto read_products; } result_header.ingredients.push_back(next_recipe_reagent(curr->left)); - if (curr->right && curr->right->atom) { - result_header.ingredients.push_back(next_recipe_reagent(curr->right)); - return result_header; // no products - } } - for (; curr && !curr->atom; curr=curr->right) + if (curr) { + assert(curr->atom); + result_header.ingredients.push_back(next_recipe_reagent(curr)); + return result_header; // no products + } + read_products: + for (/*nada*/; curr && !curr->atom; curr=curr->right) result_header.products.push_back(next_recipe_reagent(curr->left)); if (curr) { assert(curr->atom); @@ -236,8 +243,33 @@ recipe from_reagent(constreturn result_header; } -// todo: unit test: 'recipe number' vs 'recipe -> number' +:(before "End Unit Tests") +void test_from_reagent_atomic() { + reagent a("{f: recipe}"); + recipe r_header = from_reagent(a); + CHECK(r_header.ingredients.empty()); + CHECK(r_header.products.empty()); +} +void test_from_reagent_non_atomic() { + reagent a("{f: (recipe number -> number)}"); + recipe r_header = from_reagent(a); + CHECK_EQ(SIZE(r_header.ingredients), 1); + CHECK_EQ(SIZE(r_header.products), 1); +} +void test_from_reagent_reads_ingredient_at_end() { + reagent a("{f: (recipe number number)}"); + recipe r_header = from_reagent(a); + CHECK_EQ(SIZE(r_header.ingredients), 2); + CHECK(r_header.products.empty()); +} +void test_from_reagent_reads_sole_ingredient_at_end() { + reagent a("{f: (recipe number)}"); + recipe r_header = from_reagent(a); + CHECK_EQ(SIZE(r_header.ingredients), 1); + CHECK(r_header.products.empty()); +} +:(code) reagent next_recipe_reagent(const type_tree* curr) { if (!curr->left) return reagent("recipe:"+curr->name); reagent result; -- cgit 1.4.1-2-gfad0