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

VariableLengthQuantityTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import org.junit.Before;
  2. import org.junit.Ignore;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.ExpectedException;
  6. import static org.junit.Assert.assertEquals;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class VariableLengthQuantityTest {
  10. @Rule
  11. public ExpectedException expectedException = ExpectedException.none();
  12. private VariableLengthQuantity variableLengthQuantity;
  13. @Before
  14. public void setup() {
  15. variableLengthQuantity = new VariableLengthQuantity();
  16. }
  17. @Test
  18. public void testZero() {
  19. List<String> expected = Arrays.asList("0x0");
  20. List<Long> numbers = Arrays.asList(0x0L);
  21. assertEquals(expected, variableLengthQuantity.encode(numbers));
  22. }
  23. @Ignore("Remove to run test")
  24. @Test
  25. public void testArbitrarySingleByte() {
  26. List<String> expected = Arrays.asList("0x40");
  27. List<Long> numbers = Arrays.asList(0x40L);
  28. assertEquals(expected, variableLengthQuantity.encode(numbers));
  29. }
  30. @Ignore("Remove to run test")
  31. @Test
  32. public void testLargestSingleByte() {
  33. List<String> expected = Arrays.asList("0x7f");
  34. List<Long> numbers = Arrays.asList(0x7fL);
  35. assertEquals(expected, variableLengthQuantity.encode(numbers));
  36. }
  37. @Ignore("Remove to run test")
  38. @Test
  39. public void testSmallestDoubleByte() {
  40. List<String> expected = Arrays.asList("0x81", "0x0");
  41. List<Long> numbers = Arrays.asList(0x80L);
  42. assertEquals(expected, variableLengthQuantity.encode(numbers));
  43. }
  44. @Ignore("Remove to run test")
  45. @Test
  46. public void testArbitraryDoubleByte() {
  47. List<String> expected = Arrays.asList("0xc0", "0x0");
  48. List<Long> numbers = Arrays.asList(0x2000L);
  49. assertEquals(expected, variableLengthQuantity.encode(numbers));
  50. }
  51. @Ignore("Remove to run test")
  52. @Test
  53. public void testLargestDoubleByte() {
  54. List<String> expected = Arrays.asList("0xff", "0x7f");
  55. List<Long> numbers = Arrays.asList(0x3fffL);
  56. assertEquals(expected, variableLengthQuantity.encode(numbers));
  57. }
  58. @Ignore("Remove to run test")
  59. @Test
  60. public void testSmallestTripleByte() {
  61. List<String> expected = Arrays.asList("0x81", "0x80", "0x0");
  62. List<Long> numbers = Arrays.asList(0x4000L);
  63. assertEquals(expected, variableLengthQuantity.encode(numbers));
  64. }
  65. @Ignore("Remove to run test")
  66. @Test
  67. public void testArbitraryTripleByte() {
  68. List<String> expected = Arrays.asList("0xc0", "0x80", "0x0");
  69. List<Long> numbers = Arrays.asList(0x100000L);
  70. assertEquals(expected, variableLengthQuantity.encode(numbers));
  71. }
  72. @Ignore("Remove to run test")
  73. @Test
  74. public void testLargestTripleByte() {
  75. List<String> expected = Arrays.asList("0xff", "0xff", "0x7f");
  76. List<Long> numbers = Arrays.asList(0x1fffffL);
  77. assertEquals(expected, variableLengthQuantity.encode(numbers));
  78. }
  79. @Ignore("Remove to run test")
  80. @Test
  81. public void testSmallestQuadrupleByte() {
  82. List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x0");
  83. List<Long> numbers = Arrays.asList(0x200000L);
  84. assertEquals(expected, variableLengthQuantity.encode(numbers));
  85. }
  86. @Ignore("Remove to run test")
  87. @Test
  88. public void testArbitraryQuadrupleByte() {
  89. List<String> expected = Arrays.asList("0xc0", "0x80", "0x80", "0x0");
  90. List<Long> numbers = Arrays.asList(0x8000000L);
  91. assertEquals(expected, variableLengthQuantity.encode(numbers));
  92. }
  93. @Ignore("Remove to run test")
  94. @Test
  95. public void testLargestQuadrupleByte() {
  96. List<String> expected = Arrays.asList("0xff", "0xff", "0xff", "0x7f");
  97. List<Long> numbers = Arrays.asList(0xfffffffL);
  98. assertEquals(expected, variableLengthQuantity.encode(numbers));
  99. }
  100. @Ignore("Remove to run test")
  101. @Test
  102. public void testSmallestQuintupleByte() {
  103. List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x80", "0x0");
  104. List<Long> numbers = Arrays.asList(0x10000000L);
  105. assertEquals(expected, variableLengthQuantity.encode(numbers));
  106. }
  107. @Ignore("Remove to run test")
  108. @Test
  109. public void testArbitraryQuintupleByte() {
  110. List<String> expected = Arrays.asList("0x8f", "0xf8", "0x80", "0x80", "0x0");
  111. List<Long> numbers = Arrays.asList(0xff000000L);
  112. assertEquals(expected, variableLengthQuantity.encode(numbers));
  113. }
  114. @Ignore("Remove to run test")
  115. @Test
  116. public void testMaximum32BitIntegerInput() {
  117. List<String> expected = Arrays.asList("0x8f", "0xff", "0xff", "0xff", "0x7f");
  118. List<Long> numbers = Arrays.asList(0xffffffffL);
  119. assertEquals(expected, variableLengthQuantity.encode(numbers));
  120. }
  121. @Ignore("Remove to run test")
  122. @Test
  123. public void testTwoSingleByteValues() {
  124. List<String> expected = Arrays.asList("0x40", "0x7f");
  125. List<Long> numbers = Arrays.asList(0x40L, 0x7fL);
  126. assertEquals(expected, variableLengthQuantity.encode(numbers));
  127. }
  128. @Ignore("Remove to run test")
  129. @Test
  130. public void testTwoMultiByteValues() {
  131. List<String> expected = Arrays.asList("0x81", "0x80", "0x0", "0xc8", "0xe8", "0x56");
  132. List<Long> numbers = Arrays.asList(0x4000L, 0x123456L);
  133. assertEquals(expected, variableLengthQuantity.encode(numbers));
  134. }
  135. @Ignore("Remove to run test")
  136. @Test
  137. public void testManyMultiByteValues() {
  138. List<String> expected = Arrays.asList("0xc0", "0x0", "0xc8", "0xe8",
  139. "0x56", "0xff", "0xff", "0xff",
  140. "0x7f", "0x0", "0xff", "0x7f",
  141. "0x81", "0x80", "0x0");
  142. List<Long> numbers = Arrays.asList(0x2000L, 0x123456L, 0xfffffffL,
  143. 0x0L, 0x3fffL, 0x4000L);
  144. assertEquals(expected, variableLengthQuantity.encode(numbers));
  145. }
  146. @Ignore("Remove to run test")
  147. @Test
  148. public void testDecodeOneByte() {
  149. List<String> expected = Arrays.asList("0x7f");
  150. List<Long> bytes = Arrays.asList(0x7fL);
  151. assertEquals(expected, variableLengthQuantity.decode(bytes));
  152. }
  153. @Ignore("Remove to run test")
  154. @Test
  155. public void testDecodeTwoBytes() {
  156. List<String> expected = Arrays.asList("0x2000");
  157. List<Long> bytes = Arrays.asList(0xc0L, 0x0L);
  158. assertEquals(expected, variableLengthQuantity.decode(bytes));
  159. }
  160. @Ignore("Remove to run test")
  161. @Test
  162. public void testDecodeThreeBytes() {
  163. List<String> expected = Arrays.asList("0x1fffff");
  164. List<Long> bytes = Arrays.asList(0xffL, 0xffL, 0x7fL);
  165. assertEquals(expected, variableLengthQuantity.decode(bytes));
  166. }
  167. @Ignore("Remove to run test")
  168. @Test
  169. public void testDecodeFourBytes() {
  170. List<String> expected = Arrays.asList("0x200000");
  171. List<Long> bytes = Arrays.asList(0x81L, 0x80L, 0x80L, 0x0L);
  172. assertEquals(expected, variableLengthQuantity.decode(bytes));
  173. }
  174. @Ignore("Remove to run test")
  175. @Test
  176. public void testDecodeMaximum32BitInteger() {
  177. List<String> expected = Arrays.asList("0xffffffff");
  178. List<Long> bytes = Arrays.asList(0x8fL, 0xffL, 0xffL, 0xffL, 0x7fL);
  179. assertEquals(expected, variableLengthQuantity.decode(bytes));
  180. }
  181. @Ignore("Remove to run test")
  182. @Test
  183. public void testCannotDecodeIncompleteSequence() {
  184. List<Long> bytes = Arrays.asList(0xffL);
  185. expectedException.expect(IllegalArgumentException.class);
  186. expectedException.expectMessage("Invalid variable-length quantity encoding");
  187. variableLengthQuantity.decode(bytes);
  188. }
  189. @Ignore("Remove to run test")
  190. @Test
  191. public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
  192. List<Long> bytes = Arrays.asList(0x80L);
  193. expectedException.expect(IllegalArgumentException.class);
  194. expectedException.expectMessage("Invalid variable-length quantity encoding");
  195. variableLengthQuantity.decode(bytes);
  196. }
  197. @Ignore("Remove to run test")
  198. @Test
  199. public void testDecodeMultipleBytes() {
  200. List<String> expected = Arrays.asList("0x2000", "0x123456",
  201. "0xfffffff", "0x0", "0x3fff",
  202. "0x4000");
  203. List<Long> bytes = Arrays.asList(0xc0L, 0x0L, 0xc8L, 0xe8L, 0x56L,
  204. 0xffL, 0xffL, 0xffL, 0x7fL, 0x0L,
  205. 0xffL, 0x7fL, 0x81L, 0x80L, 0x0L);
  206. assertEquals(expected, variableLengthQuantity.decode(bytes));
  207. }
  208. }