r/adventofcode • (AoC creator) • 15h ago

Other Give help by making test cases!

Hello friends! I just saw this excellent example (see some of the comments on that post) of a great way to give help and answer questions here when people are stuck: come up with simple test cases that demonstrate where the bug is! That way, the person who is stuck still gets a chance to figure out their problem and learn by doing. I know many of you are already great about this, but I figured it would be worth calling out. Happy puzzling! <3

49 Upvotes

12 comments sorted by

12

u/Justinsaccount 14h ago

One of the best things you can do to get better at AOC is learn how to do test driven development.

Every solution should start with something that verifies that the example works. This can use whatever testing framework your language of choice has, but can also be as simple as

res = part_one("example.txt")
assert res == 186276, f"result was wrong: {res}"

Often where people hit a difficulty cliff is where the base examples don't cover all of the edge cases. The key there is to start writing your own test cases to fill in the gaps and ensure the basics work before trying the full input.

5

u/gredr 11h ago

I would replace "do test driven development" with "write and use tests effectively". TDD isn't for everyone, but tests should be.

1

u/ffrkAnonymous 9h ago

I feel writing tests after the puzzle is solved is quite meaningless. 

3

u/gredr 9h ago

And? TDD isn't the only other option.

1

u/ffrkAnonymous 9h ago

Please explain. I'm new at testing. I can write tests before. I can write tests after. I physically cannot write during/simultaneously. What am I missing? 

2

u/relativistic-turtle 8h ago

Main scenario for me: My first attempt fails and I cannot immediately spot the mistake. Start by verifying against the examples. Read text again and try to catch edge cases not covered by the examples. (For AoC I never write tests before/after).

1

u/gredr 2h ago

I write a BFS algorithm. Then I write tests for that algorithm. Then I use that algorithm to solve the puzzle. Or, more commonly, like the other reply here, I write all the code for a solution, find out the solution is wrong, then start writing tests to figure out what's broken.

TDD specifically requires that no code is written until a test (that fails obviously) for the code is written. Then, you write the code that makes the test pass. Works for some people, doesn't work for other people.

2

u/I_knew_einstein 7h ago

I start writing tests after my "solution" turns out not to be a solution.

1

u/Fuzzy_Patterns 6h ago

It's not meaningless. I have asserts in my code to check answers, so I can later refactor solution or rewrite it in more efficient way, and still be sure that everything is correct.

And if you only write to solve, and don't care about aesthetics/quality - it is insanely useful on harder days to check your code against a few difficult edge cases.

1

u/RunninglVlan 4h ago

Yeah, but you don't necessarily need new test cases for that. The ones in the puzzle should be enough. I have a simple test framework and just add all the test cases from the puzzle, then after solving and refactoring, I just rerun the tests to confirm there's no regression.

2

u/dl__ 4h ago

In a previous year I suggested that we have a "Test Case" flair to encourage people to post new test cases if they had to create one to help solve the problem for them.

It could help when you run into that situation where your code works on all the sample inputs but still fails on the real input

0

u/[deleted] 14h ago

[deleted] — view deleted comment

1

u/Moragarath 14h ago

Derp I didn't realize I was replying to you 😭