Skip to content

exercises(all-your-base): test: add test for global array #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2023

Conversation

ee7
Copy link
Member

@ee7 ee7 commented Sep 14, 2023

Follow-up from #317 (comment).


Commit b65162f added missing frees in the test cases that returned a single 0. This causes a previously-passing solution that incorrectly returned a reference to a global array to panic:

$ zig test test_all_your_base.zig
Test [9/17] test.empty list... thread 27369 panic: Invalid free
/usr/lib/zig/std/heap/general_purpose_allocator.zig:609:21: 0x22cb5e in freeLarge (test)
                    @panic("Invalid free");
                    ^
/usr/lib/zig/std/heap/general_purpose_allocator.zig:824:31: 0x22c0db in free (test)
                self.freeLarge(old_mem, log2_old_align, ret_addr);
                              ^
/usr/lib/zig/std/mem/Allocator.zig:98:28: 0x22800f in free__anon_3776 (test)
    return self.vtable.free(self.ptr, buf, log2_buf_align, ret_addr);
                           ^
/home/foo/exercism-zig/exercises/practice/all-your-base/test_all_your_base.zig:94:33: 0x22881a in test.empty list (test)
    defer testing.allocator.free(actual);
                                ^

Add a targeted test case to help a user that writes such a solution. Their solution will still produce a panic, but it will point to a more obvious test case. And it will now produce a test failure first:

$ zig test test_all_your_base.zig
Test [9/18] test.empty list - second call returns different memory... slices differ. first difference occurs at index 0 (0x0)

============ expected this output: =============  len: 1 (0x1)

[0]: 0

============= instead found this: ==============  len: 1 (0x1)

[0]: 1

================================================

thread 27306 panic: Invalid free
/usr/lib/zig/std/heap/general_purpose_allocator.zig:609:21: 0x22ceee in freeLarge (test)
                    @panic("Invalid free");
                    ^
/usr/lib/zig/std/heap/general_purpose_allocator.zig:824:31: 0x22c46b in free (test)
                self.freeLarge(old_mem, log2_old_align, ret_addr);
                              ^
/usr/lib/zig/std/mem/Allocator.zig:98:28: 0x2280df in free__anon_3777 (test)
    return self.vtable.free(self.ptr, buf, log2_buf_align, ret_addr);
                           ^
/home/foo/exercism-zig/exercises/practice/all-your-base/test_all_your_base.zig:101:33: 0x228a3e in test.empty list - second call returns different memory (test)
    defer testing.allocator.free(again);
                                ^

The ordering of the test cases looks strange: the test that calls convert twice with empty digits is before the test that does so once. But otherwise the user will see the panic in the non-targeted test case.

A recent commit [1] added missing frees in the test cases that returned
a single 0. This causes a previously-passing solution that returned a
slice referencing a global array to panic:

    $ zig test test_all_your_base.zig
    Test [9/17] test.empty list... thread 27369 panic: Invalid free
    /usr/lib/zig/std/heap/general_purpose_allocator.zig:609:21: 0x22cb5e in freeLarge (test)
                        @Panic("Invalid free");
                        ^
    /usr/lib/zig/std/heap/general_purpose_allocator.zig:824:31: 0x22c0db in free (test)
                    self.freeLarge(old_mem, log2_old_align, ret_addr);
                                ^
    /usr/lib/zig/std/mem/Allocator.zig:98:28: 0x22800f in free__anon_3776 (test)
        return self.vtable.free(self.ptr, buf, log2_buf_align, ret_addr);
                            ^
    /home/foo/exercism-zig/exercises/practice/all-your-base/test_all_your_base.zig:94:33: 0x22881a in test.empty list (test)
        defer testing.allocator.free(actual);
                                    ^

Add a targeted test case to help a user that writes such a solution.
Their solution will still produce a panic, but it will point to a more
obvious test case. And it will now produce a test failure first:

    $ zig test test_all_your_base.zig
    Test [9/18] test.empty list - second call returns different memory... slices differ. first difference occurs at index 0 (0x0)

    ============ expected this output: =============  len: 1 (0x1)

    [0]: 0

    ============= instead found this: ==============  len: 1 (0x1)

    [0]: 1

    ================================================

    thread 27306 panic: Invalid free
    /usr/lib/zig/std/heap/general_purpose_allocator.zig:609:21: 0x22ceee in freeLarge (test)
                        @Panic("Invalid free");
                        ^
    /usr/lib/zig/std/heap/general_purpose_allocator.zig:824:31: 0x22c46b in free (test)
                    self.freeLarge(old_mem, log2_old_align, ret_addr);
                                ^
    /usr/lib/zig/std/mem/Allocator.zig:98:28: 0x2280df in free__anon_3777 (test)
        return self.vtable.free(self.ptr, buf, log2_buf_align, ret_addr);
                            ^
    /home/foo/exercism-zig/exercises/practice/all-your-base/test_all_your_base.zig:101:33: 0x228a3e in test.empty list - second call returns different memory (test)
        defer testing.allocator.free(again);
                                    ^

The ordering of the test cases looks strange: the test that calls
`convert` twice with empty `digits` is before the test that does so
once. But otherwise the user will see the panic in the non-targeted test
case.

[1] b65162f, 2023-09-12, "exercises(all-your-base): add missing frees"
ee7 added 2 commits September 16, 2023 10:04
This test's name starts with "empty list", and appears before tests
named (also somewhat badly) "empty list" and "single zero".
@ee7 ee7 merged commit ee70fc1 into exercism:main Sep 16, 2023
@ee7 ee7 deleted the exercises-all-your-base-add-test branch September 16, 2023 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants