Repository navigation
circular-buffer: add to track - #682
Conversation
|
Thanks for contributing @vivshaw! :D Sorry for messing with the PR name, it's just to make it consistent so that it's easy to see when someone is working on a new exercise :) Once you're ready for it to be reviewed, feel free to change it to "circular-buffer: add to track". That way it's easy for us to tell when you want a review so that we don't start leaving comments while you're still working on it :) |
|
Alright, finished implementing all canonical test cases & the build passes. The one big question I have is, for the exceptions that we want the CircularBuffer to throw when reading an empty buffer, should we use an existing exception type (and if so, which), or expect the user to implement a custom BufferEmptyException and BufferFullException? I went with the latter, because learning how to implement your own exceptions is useful, but I'm worried it'll make the exercise overly complex to newcomers who are inexperienced with exception handling. |
|
What I've done before is make those exception classes and including them in both the example implementation and the starter implementation (see the bank account exercise for an example). That way the users get experience with custom exceptions but, like you said, the exercise doesn't get too complex :) |
|
@FridaTveit Alright, I've just done that & updated the test suite accordingly. Let me know if anything else needs changed. |
Smarticles101
left a comment
There was a problem hiding this comment.
I looked at this and left a few comments that mention some minor things I saw. Other than that it looks good.
| "difficulty": 8, | ||
| "topics": [ | ||
| "Queues", | ||
| "Classes", |
There was a problem hiding this comment.
It looks like we're going to be adding topics for every exercise soon, so I'm a +1 to what @FridaTveit suggested :)
In order to prepare your track for the new track progression model, you'll need to do the following:
- add topics to each exercise. We have a list of topics that you can use for inspiration, but this list is not exhaustive. The topics you choose should make sense for your language.
- add a rough guess at the difficulty of each exercise.
- choose 10-20 exercises to mark as core
- give the core exercises an order
- for all other exercises, mark them as
unlocked_byone of the core exercises (identified by the slug)
While we shouldn't embark on the above en masse just yet, adding topics is therefore good future-proofing!
| @@ -0,0 +1,161 @@ | |||
| import static junit.framework.TestCase.assertEquals; | |||
There was a problem hiding this comment.
All the other exercises use import static org.junit.Assert.assertEquals;
| import org.junit.rules.ExpectedException; | ||
|
|
||
| public class CircularBufferTest { | ||
| @Rule |
There was a problem hiding this comment.
Usually there is a blank line after the class declaration.
| @@ -0,0 +1,5 @@ | |||
| public class BufferIOException extends RuntimeException { | |||
There was a problem hiding this comment.
This should extend Exception instead of RuntimeException. RuntimeException should be for errors you can't fix, generally programmer errors where the best thing to do is for the program to crash. If you want the exception to be handled then extend Exception because the compiler will force you to handle the exception. See this article for some more information about it :)
|
Thanks for the feedback! I've just finished implementing all the changes @Smarticles101 and @FridaTveit requested. @FridaTveit, thanks in particular for jogging my memory on |
Smarticles101
left a comment
There was a problem hiding this comment.
I believe this is good now. Thanks :)
FridaTveit
left a comment
There was a problem hiding this comment.
I just have a small question about your asserts in the test, but apart from that I agree with @Smarticles101, this is good to go :) Great work! :D
| CircularBuffer<Integer> buffer = new CircularBuffer<>(1); | ||
|
|
||
| buffer.write(1); | ||
| assertEquals((int) buffer.read(), 1); |
There was a problem hiding this comment.
Why do you need this cast?
There was a problem hiding this comment.
Also these are the wrong way around. It's assertEquals(expected, actual). I get that wrong all the time too :P
There was a problem hiding this comment.
I thought the same thing about the cast the first time I looked at it so I ran it and found out why.
error: reference to assertEquals is ambiguous
assertEquals(buffer.read(), 1);
^
both method assertEquals(long,long) in Assert and method assertEquals(Object,Object) in Assert match
There was a problem hiding this comment.
@FridaTveit, the int cast was indeed due to the error that @Smarticles101 posted. I wrestled with that int cast for a couple hours & just now figured out a) why it does that and b) how to avoid it! It seems to be due to some wackiness with how Java autoboxing & unboxing interacts with target type inference.
The problem goes like this: Since Java Collections can't store raw types, any method we use to pull values out of our data structure will return Integer rather than int. So, my line return buffer.remove() returns an Integer. This then becomes an argument to assertEquals(), but the other argument is an int. assertEquals() is overloaded, but doesn't have any implementation with the type signature Integer, int, so it's stuck deciding between its two best guesses for the type signature: long, long and Object, Object.
So if we stick with assertEquals(), we can either: 1) remove the cast, in which case any users trying to implement the exercise with a Collection may run into this confusing bug, or 2) kludge both the arguments to the same type with a cast so they're both unboxed or both boxed.
But, assertThat() to the rescue! Turns out that asssertThat(Integer i, is(int j)) is unambiguously typed & neatly sidesteps the problem. So I redid the tests with that & we should be golden, I think.
…ugs when testing equality for Integers
Smarticles101
left a comment
There was a problem hiding this comment.
Glad to see that you've got this figured out! I like the use of assertThat. It reads more like a sentence to me. Anyways, this is ready to merge! Thanks for the hard work @vivshaw :)
test spec directory
Reviewer Resources:
Track Policies