lots of exercises in java... from https://github.com/exercism/java

BinaryTest.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import org.junit.Test;
  2. import org.junit.Ignore;
  3. import org.junit.runner.RunWith;
  4. import org.junit.runners.Parameterized;
  5. import org.junit.runners.Parameterized.Parameters;
  6. import java.util.Arrays;
  7. import java.util.Collection;
  8. import static org.junit.Assert.assertEquals;
  9. @RunWith(Parameterized.class)
  10. public class BinaryTest {
  11. private String binaryNumberAsString;
  12. private int decimalNumber;
  13. @Parameters(name = "{index}: expected {1} when converting \"{0}\" from binary to decimal.")
  14. public static Collection<Object[]> data() {
  15. return Arrays.asList(new Object[][]{
  16. {"1", 1},
  17. {"10", 2},
  18. {"11", 3},
  19. {"100", 4},
  20. {"1001", 9},
  21. {"11010", 26},
  22. {"10001101000", 1128},
  23. {"2", 0},
  24. {"5", 0},
  25. {"9", 0},
  26. {"134678", 0},
  27. {"abc10z", 0},
  28. {"011", 3}
  29. });
  30. }
  31. public BinaryTest(String binaryNumberAsString, int decimalNumber) {
  32. this.binaryNumberAsString = binaryNumberAsString;
  33. this.decimalNumber = decimalNumber;
  34. }
  35. @Test
  36. public void test() {
  37. Binary binary = new Binary(binaryNumberAsString);
  38. assertEquals(decimalNumber, binary.getDecimal());
  39. }
  40. }