problem-specifications icon indicating copy to clipboard operation
problem-specifications copied to clipboard

[Binary Search Tree][c language] new test case

Open damaxi opened this issue 4 years ago • 5 comments

Inspired by @siebenschlaefer comment https://exercism.io/my/solutions/b6f9db46186547ba9a7bbebf3f193078?iteration_idx=1

It looks like there is a missing test case:

   int tree_data[] = { 3, 1, 2 };
   node_t *tree = build_tree(
      tree_data, ARRAY_SIZE(tree_data));

   int expected[] = { 1, 2, 3 };
   int *actual = sorted_data(tree);
   TEST_ASSERT_EQUAL_INT_ARRAY(
      expected, actual,
      ARRAY_SIZE(expected));

   free_tree(tree);
   free(actual);

I had created initially a wrong algorithm for the left and right branches. Where the left branch passe because there was a missing test case, while the right branch I fixed. Adding this test case will help to write a proper implementation.

damaxi avatar Jul 29 '21 06:07 damaxi

I cannot see the comment on the exercise (probably because I am not a mentor). Can you copy/screenshot the comment here, for context?

wolf99 avatar Jul 30 '21 10:07 wolf99

Sure here you are: sorted_data() calls fill_data() which itself calls left_branch() for the left sub-tree and right_branch() for the right sub-tree.

Those two functions left_branch() and right_branch() look slightly different, in fact one of them does not do what it should.

You could try to analyze them to find the issue, or you could try this (additional) test case:

static void test_for_damaxi(void)
{
   int tree_data[] = { 3, 1, 2 };
   node_t *tree = build_tree(
      tree_data, ARRAY_SIZE(tree_data));

   int expected[] = { 1, 2, 3 };
   int *actual = sorted_data(tree);
   TEST_ASSERT_EQUAL_INT_ARRAY(
      expected, actual,
      ARRAY_SIZE(expected));

   free_tree(tree);
   free(actual);
}

Let me know if you need another hint or some less cryptic help.

damaxi avatar Jul 30 '21 11:07 damaxi

@wolf99 Perhaps you might be able to access https://exercism.io/mentor/solutions/b6f9db46186547ba9a7bbebf3f193078?iteration_idx=1

There are five tests for "can sort data": one for 1 element, three for 2 elements, and one for a "complex tree" (see canonical-data.json). The tree for that last test looks like this:

  2
+-+-+
1   3
    +---+
        6
      +-+-+
      5   7

In that particular solution there were separate functions for the left and right subtree of the root. The right subtree was handled correctly and the left subtree was not complex enough to detect the error.

We could add some notes in that example, or we could add another test with a different "complex" tree.

siebenschlaefer avatar Jul 30 '21 11:07 siebenschlaefer

If you would like to propose another tree that should be tested, please share here and a PR can be made with that

wolf99 avatar Oct 14 '21 20:10 wolf99

@damaxi See the comment by @wolf99 above: do you have another tree that would make for a good test case? If so, could you share it here?

ErikSchierboom avatar Jan 13 '22 15:01 ErikSchierboom