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

BaseConverterTest.java 9.1KB

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