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

BaseConverterTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import org.junit.Ignore;
  2. import org.junit.Rule;
  3. import org.junit.Test;
  4. import org.junit.rules.ExpectedException;
  5. import java.util.Arrays;
  6. import static org.junit.Assert.assertArrayEquals;
  7. public class BaseConverterTest {
  8. @Rule
  9. public ExpectedException expectedException = ExpectedException.none();
  10. @Test
  11. public void testSingleBitOneToDecimal() {
  12. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1});
  13. final int[] expectedDigits = new int[]{1};
  14. final int[] actualDigits = baseConverter.convertToBase(10);
  15. assertArrayEquals(
  16. String.format(
  17. "Expected digits: %s but found digits: %s",
  18. Arrays.toString(expectedDigits),
  19. Arrays.toString(actualDigits)),
  20. expectedDigits,
  21. actualDigits);
  22. }
  23. @Ignore("Remove to run test")
  24. @Test
  25. public void testBinaryToSingleDecimal() {
  26. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1, 0, 1});
  27. final int[] expectedDigits = new int[]{5};
  28. final int[] actualDigits = baseConverter.convertToBase(10);
  29. assertArrayEquals(
  30. String.format(
  31. "Expected digits: %s but found digits: %s",
  32. Arrays.toString(expectedDigits),
  33. Arrays.toString(actualDigits)),
  34. expectedDigits,
  35. actualDigits);
  36. }
  37. @Ignore("Remove to run test")
  38. @Test
  39. public void testSingleDecimalToBinary() {
  40. final BaseConverter baseConverter = new BaseConverter(10, new int[]{5});
  41. final int[] expectedDigits = new int[]{1, 0, 1};
  42. final int[] actualDigits = baseConverter.convertToBase(2);
  43. assertArrayEquals(
  44. String.format(
  45. "Expected digits: %s but found digits: %s",
  46. Arrays.toString(expectedDigits),
  47. Arrays.toString(actualDigits)),
  48. expectedDigits,
  49. actualDigits);
  50. }
  51. @Ignore("Remove to run test")
  52. @Test
  53. public void testBinaryToMultipleDecimal() {
  54. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0});
  55. final int[] expectedDigits = new int[]{4, 2};
  56. final int[] actualDigits = baseConverter.convertToBase(10);
  57. assertArrayEquals(
  58. String.format(
  59. "Expected digits: %s but found digits: %s",
  60. Arrays.toString(expectedDigits),
  61. Arrays.toString(actualDigits)),
  62. expectedDigits,
  63. actualDigits);
  64. }
  65. @Ignore("Remove to run test")
  66. @Test
  67. public void testDecimalToBinary() {
  68. final BaseConverter baseConverter = new BaseConverter(10, new int[]{4, 2});
  69. final int[] expectedDigits = new int[]{1, 0, 1, 0, 1, 0};
  70. final int[] actualDigits = baseConverter.convertToBase(2);
  71. assertArrayEquals(
  72. String.format(
  73. "Expected digits: %s but found digits: %s",
  74. Arrays.toString(expectedDigits),
  75. Arrays.toString(actualDigits)),
  76. expectedDigits,
  77. actualDigits);
  78. }
  79. @Ignore("Remove to run test")
  80. @Test
  81. public void testTrinaryToHexadecimal() {
  82. final BaseConverter baseConverter = new BaseConverter(3, new int[]{1, 1, 2, 0});
  83. final int[] expectedDigits = new int[]{2, 10};
  84. final int[] actualDigits = baseConverter.convertToBase(16);
  85. assertArrayEquals(
  86. String.format(
  87. "Expected digits: %s but found digits: %s",
  88. Arrays.toString(expectedDigits),
  89. Arrays.toString(actualDigits)),
  90. expectedDigits,
  91. actualDigits);
  92. }
  93. @Ignore("Remove to run test")
  94. @Test
  95. public void testHexadecimalToTrinary() {
  96. final BaseConverter baseConverter = new BaseConverter(16, new int[]{2, 10});
  97. final int[] expectedDigits = new int[]{1, 1, 2, 0};
  98. final int[] actualDigits = baseConverter.convertToBase(3);
  99. assertArrayEquals(
  100. String.format(
  101. "Expected digits: %s but found digits: %s",
  102. Arrays.toString(expectedDigits),
  103. Arrays.toString(actualDigits)),
  104. expectedDigits,
  105. actualDigits);
  106. }
  107. @Ignore("Remove to run test")
  108. @Test
  109. public void test15BitInteger() {
  110. final BaseConverter baseConverter = new BaseConverter(97, new int[]{3, 46, 60});
  111. final int[] expectedDigits = new int[]{6, 10, 45};
  112. final int[] actualDigits = baseConverter.convertToBase(73);
  113. assertArrayEquals(
  114. String.format(
  115. "Expected digits: %s but found digits: %s",
  116. Arrays.toString(expectedDigits),
  117. Arrays.toString(actualDigits)),
  118. expectedDigits,
  119. actualDigits);
  120. }
  121. @Ignore("Remove to run test")
  122. @Test
  123. public void testEmptyDigits() {
  124. expectedException.expect(IllegalArgumentException.class);
  125. expectedException.expectMessage("You must supply at least one digit.");
  126. new BaseConverter(2, new int[]{});
  127. }
  128. @Ignore("Remove to run test")
  129. @Test
  130. public void testSingleZero() {
  131. final BaseConverter baseConverter = new BaseConverter(10, new int[]{0});
  132. final int[] expectedDigits = new int[]{0};
  133. final int[] actualDigits = baseConverter.convertToBase(2);
  134. assertArrayEquals(
  135. String.format(
  136. "Expected digits: %s but found digits: %s",
  137. Arrays.toString(expectedDigits),
  138. Arrays.toString(actualDigits)),
  139. expectedDigits,
  140. actualDigits);
  141. }
  142. @Ignore("Remove to run test")
  143. @Test
  144. public void testMultipleZeros() {
  145. expectedException.expect(IllegalArgumentException.class);
  146. expectedException.expectMessage("Digits may not contain leading zeros.");
  147. new BaseConverter(10, new int[]{0, 0, 0});
  148. }
  149. @Ignore("Remove to run test")
  150. @Test
  151. public void testLeadingZeros() {
  152. expectedException.expect(IllegalArgumentException.class);
  153. expectedException.expectMessage("Digits may not contain leading zeros.");
  154. new BaseConverter(7, new int[]{0, 6, 0});
  155. }
  156. @Ignore("Remove to run test")
  157. @Test
  158. public void testFirstBaseIsOne() {
  159. expectedException.expect(IllegalArgumentException.class);
  160. expectedException.expectMessage("Bases must be at least 2.");
  161. new BaseConverter(1, new int[]{});
  162. }
  163. @Ignore("Remove to run test")
  164. @Test
  165. public void testFirstBaseIsZero() {
  166. expectedException.expect(IllegalArgumentException.class);
  167. expectedException.expectMessage("Bases must be at least 2.");
  168. new BaseConverter(0, new int[]{});
  169. }
  170. @Ignore("Remove to run test")
  171. @Test
  172. public void testFirstBaseIsNegative() {
  173. expectedException.expect(IllegalArgumentException.class);
  174. expectedException.expectMessage("Bases must be at least 2.");
  175. new BaseConverter(-2, new int[]{});
  176. }
  177. @Ignore("Remove to run test")
  178. @Test
  179. public void testNegativeDigit() {
  180. expectedException.expect(IllegalArgumentException.class);
  181. expectedException.expectMessage("Digits may not be negative.");
  182. new BaseConverter(2, new int[]{1, -1, 1, 0, 1, 0});
  183. }
  184. @Ignore("Remove to run test")
  185. @Test
  186. public void testInvalidPositiveDigit() {
  187. expectedException.expect(IllegalArgumentException.class);
  188. expectedException.expectMessage("All digits must be strictly less than the base.");
  189. new BaseConverter(2, new int[]{1, 2, 1, 0, 1, 0});
  190. }
  191. @Ignore("Remove to run test")
  192. @Test
  193. public void testSecondBaseIsOne() {
  194. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0});
  195. expectedException.expect(IllegalArgumentException.class);
  196. expectedException.expectMessage("Bases must be at least 2.");
  197. baseConverter.convertToBase(1);
  198. }
  199. @Ignore("Remove to run test")
  200. @Test
  201. public void testSecondBaseIsZero() {
  202. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1, 0, 1, 0, 1, 0});
  203. expectedException.expect(IllegalArgumentException.class);
  204. expectedException.expectMessage("Bases must be at least 2.");
  205. baseConverter.convertToBase(0);
  206. }
  207. @Ignore("Remove to run test")
  208. @Test
  209. public void testSecondBaseIsNegative() {
  210. final BaseConverter baseConverter = new BaseConverter(2, new int[]{1});
  211. expectedException.expect(IllegalArgumentException.class);
  212. expectedException.expectMessage("Bases must be at least 2.");
  213. baseConverter.convertToBase(-7);
  214. }
  215. }