/yagw2api.pom/yagw2api.common/src/test/java/yagw2api/common/tuple/TestIntTuple2.java

https://bitbucket.org/Lul4n/yagw2api · Java · 415 lines · 314 code · 82 blank · 19 comment · 2 complexity · d94d081a4c42555d22c407e7a2bc217c MD5 · raw file

  1. package yagw2api.common.tuple;
  2. /*
  3. * @formatter:off<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. * YAGW2API-Commons
  5. * _____________________________________________________________
  6. * Copyright (C) 2012 - 2015 Julian Stitz
  7. * _____________________________________________________________
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>@formatter:on
  20. */
  21. import static org.hamcrest.Matchers.is;
  22. import static org.junit.Assert.assertThat;
  23. import java.util.List;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.junit.runners.Parameterized;
  27. import org.junit.runners.Parameterized.Parameters;
  28. import de.justi.yagw2api.common.tuple.DoubleTuple2;
  29. import de.justi.yagw2api.common.tuple.FloatTuple2;
  30. import de.justi.yagw2api.common.tuple.IntTuple2;
  31. import de.justi.yagw2api.common.tuple.LongTuple2;
  32. import de.justi.yagw2api.common.tuple.Tuples;
  33. @RunWith(Parameterized.class)
  34. public class TestIntTuple2 {
  35. @Parameters(name = "v1={0}, v2={1}")
  36. public static final Object[][] paramters() {
  37. return new Object[][] {
  38. new Integer[] { 0, 0 },
  39. new Integer[] { -1, 0 },
  40. new Integer[] { 0, -1 },
  41. new Integer[] { -1, -1 },
  42. new Integer[] { 1, 0 },
  43. new Integer[] { 0, 1 },
  44. new Integer[] { 1, 1 },
  45. new Integer[] { -1, 1 },
  46. new Integer[] { 1, -1 },
  47. new Integer[] { 0, Integer.MIN_VALUE },
  48. new Integer[] { Integer.MIN_VALUE, 0 },
  49. new Integer[] { Integer.MIN_VALUE, Integer.MIN_VALUE },
  50. new Integer[] { 0, Integer.MAX_VALUE },
  51. new Integer[] { Integer.MAX_VALUE, 0 },
  52. new Integer[] { Integer.MAX_VALUE, Integer.MAX_VALUE },
  53. new Integer[] { Integer.MIN_VALUE, Integer.MAX_VALUE } };
  54. }
  55. private final int v1;
  56. private final int v2;
  57. public TestIntTuple2(final int v1, final int v2) {
  58. this.v1 = v1;
  59. this.v2 = v2;
  60. }
  61. @Test
  62. public void testClampToZero() {
  63. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  64. final IntTuple2 clamped = tuple2.clampTuple2(0, 0);
  65. assertThat(clamped.v1(), is(0));
  66. assertThat(clamped.v2(), is(0));
  67. }
  68. @Test(expected = IllegalArgumentException.class)
  69. public void testClampToInvalid() {
  70. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  71. final IntTuple2 clamped = tuple2.clampTuple2(1, -1);
  72. assertThat(clamped.v1(), is(0));
  73. assertThat(clamped.v2(), is(0));
  74. }
  75. @Test
  76. public void testClampToMaxInt() {
  77. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  78. final IntTuple2 clamped = tuple2.clampTuple2(Integer.MAX_VALUE, Integer.MAX_VALUE);
  79. assertThat(clamped.v1(), is(Integer.MAX_VALUE));
  80. assertThat(clamped.v2(), is(Integer.MAX_VALUE));
  81. }
  82. @Test
  83. public void testClampToMinInt() {
  84. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  85. final IntTuple2 clamped = tuple2.clampTuple2(Integer.MIN_VALUE, Integer.MIN_VALUE);
  86. assertThat(clamped.v1(), is(Integer.MIN_VALUE));
  87. assertThat(clamped.v2(), is(Integer.MIN_VALUE));
  88. }
  89. @Test
  90. public void testClampToIntRange() {
  91. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  92. final IntTuple2 clamped = tuple2.clampTuple2(Integer.MIN_VALUE, Integer.MAX_VALUE);
  93. assertThat(clamped.v1(), is(this.v1));
  94. assertThat(clamped.v2(), is(this.v2));
  95. }
  96. @Test
  97. public void testClampToV1() {
  98. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  99. final IntTuple2 clamped = tuple2.clampTuple2(this.v1, this.v1);
  100. assertThat(clamped.v1(), is(this.v1));
  101. assertThat(clamped.v2(), is(this.v1));
  102. }
  103. @Test
  104. public void testClampToV2() {
  105. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  106. final IntTuple2 clamped = tuple2.clampTuple2(this.v2, this.v2);
  107. assertThat(clamped.v1(), is(this.v2));
  108. assertThat(clamped.v2(), is(this.v2));
  109. }
  110. @Test
  111. public void testClampToOriginalRange() {
  112. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  113. final int lower;
  114. final int upper;
  115. if (this.v1 <= this.v2) {
  116. lower = this.v1;
  117. upper = this.v2;
  118. } else {
  119. lower = this.v2;
  120. upper = this.v1;
  121. }
  122. final IntTuple2 clamped = tuple2.clampTuple2(lower, upper);
  123. assertThat(clamped.v1(), is(this.v1));
  124. assertThat(clamped.v2(), is(this.v2));
  125. }
  126. @Test
  127. public void testMultiplyByMinusOne() {
  128. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  129. final IntTuple2 multiplied = tuple2.multiplyTuple2(-1);
  130. assertThat(multiplied.v1(), is(-tuple2.v1()));
  131. assertThat(multiplied.v2(), is(-tuple2.v2()));
  132. }
  133. @Test
  134. public void testMultiplyByZero() {
  135. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  136. final IntTuple2 multiplied = tuple2.multiplyTuple2(0);
  137. assertThat(multiplied.v1(), is(0));
  138. assertThat(multiplied.v2(), is(0));
  139. }
  140. @Test
  141. public void testMultiplyByOne() {
  142. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  143. final IntTuple2 multiplied = tuple2.multiplyTuple2(1);
  144. assertThat(multiplied.v1(), is(tuple2.v1()));
  145. assertThat(multiplied.v2(), is(tuple2.v2()));
  146. }
  147. @Test
  148. public void testMultiplyByTwo() {
  149. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  150. final IntTuple2 multiplied = tuple2.multiplyTuple2(2);
  151. assertThat(multiplied.v1(), is(2 * tuple2.v1()));
  152. assertThat(multiplied.v2(), is(2 * tuple2.v2()));
  153. }
  154. @Test
  155. public void testMultiplyByIntMin() {
  156. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  157. final IntTuple2 multiplied = tuple2.multiplyTuple2(Integer.MIN_VALUE);
  158. assertThat(multiplied.v1(), is(Integer.MIN_VALUE * tuple2.v1()));
  159. assertThat(multiplied.v2(), is(Integer.MIN_VALUE * tuple2.v2()));
  160. }
  161. @Test
  162. public void testMultiplyByIntMax() {
  163. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  164. final IntTuple2 multiplied = tuple2.multiplyTuple2(Integer.MAX_VALUE);
  165. assertThat(multiplied.v1(), is(Integer.MAX_VALUE * tuple2.v1()));
  166. assertThat(multiplied.v2(), is(Integer.MAX_VALUE * tuple2.v2()));
  167. }
  168. @Test
  169. public void testMultiplyByIntMinMax() {
  170. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  171. final IntTuple2 multiplied = tuple2.multiplyTuple2(Integer.MIN_VALUE, Integer.MAX_VALUE);
  172. assertThat(multiplied.v1(), is(Integer.MIN_VALUE * tuple2.v1()));
  173. assertThat(multiplied.v2(), is(Integer.MAX_VALUE * tuple2.v2()));
  174. }
  175. @Test
  176. public void testMultiplyByLongMin() {
  177. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  178. final LongTuple2 multiplied = tuple2.multiplyTuple2(Long.MIN_VALUE);
  179. assertThat(multiplied.v1(), is(Long.MIN_VALUE * tuple2.v1()));
  180. assertThat(multiplied.v2(), is(Long.MIN_VALUE * tuple2.v2()));
  181. }
  182. @Test
  183. public void testMultiplyByLongMax() {
  184. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  185. final LongTuple2 multiplied = tuple2.multiplyTuple2(Long.MAX_VALUE);
  186. assertThat(multiplied.v1(), is(Long.MAX_VALUE * tuple2.v1()));
  187. assertThat(multiplied.v2(), is(Long.MAX_VALUE * tuple2.v2()));
  188. }
  189. @Test
  190. public void testMultiplyByLongMinMax() {
  191. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  192. final LongTuple2 multiplied = tuple2.multiplyTuple2(Long.MIN_VALUE, Long.MAX_VALUE);
  193. assertThat(multiplied.v1(), is(Long.MIN_VALUE * tuple2.v1()));
  194. assertThat(multiplied.v2(), is(Long.MAX_VALUE * tuple2.v2()));
  195. }
  196. @Test
  197. public void testMultiplyByDoubleMin() {
  198. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  199. final DoubleTuple2 multiplied = tuple2.multiplyTuple2(Double.MIN_VALUE);
  200. assertThat(multiplied.v1(), is(Double.MIN_VALUE * tuple2.v1()));
  201. assertThat(multiplied.v2(), is(Double.MIN_VALUE * tuple2.v2()));
  202. }
  203. @Test
  204. public void testMultiplyByDoubleMax() {
  205. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  206. final DoubleTuple2 multiplied = tuple2.multiplyTuple2(Double.MAX_VALUE);
  207. assertThat(multiplied.v1(), is(Double.MAX_VALUE * tuple2.v1()));
  208. assertThat(multiplied.v2(), is(Double.MAX_VALUE * tuple2.v2()));
  209. }
  210. @Test
  211. public void testMultiplyByDoubleMinMax() {
  212. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  213. final DoubleTuple2 multiplied = tuple2.multiplyTuple2(Double.MIN_VALUE, Double.MAX_VALUE);
  214. assertThat(multiplied.v1(), is(Double.MIN_VALUE * tuple2.v1()));
  215. assertThat(multiplied.v2(), is(Double.MAX_VALUE * tuple2.v2()));
  216. }
  217. @Test
  218. public void testMultiplyByFloatMin() {
  219. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  220. final FloatTuple2 multiplied = tuple2.multiplyTuple2(Float.MIN_VALUE);
  221. assertThat(multiplied.v1(), is(Float.MIN_VALUE * tuple2.v1()));
  222. assertThat(multiplied.v2(), is(Float.MIN_VALUE * tuple2.v2()));
  223. }
  224. @Test
  225. public void testMultiplyByFloatMax() {
  226. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  227. final FloatTuple2 multiplied = tuple2.multiplyTuple2(Float.MAX_VALUE);
  228. assertThat(multiplied.v1(), is(Float.MAX_VALUE * tuple2.v1()));
  229. assertThat(multiplied.v2(), is(Float.MAX_VALUE * tuple2.v2()));
  230. }
  231. @Test
  232. public void testMultiplyByFloatMinMax() {
  233. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  234. final FloatTuple2 multiplied = tuple2.multiplyTuple2(Float.MIN_VALUE, Float.MAX_VALUE);
  235. assertThat(multiplied.v1(), is(Float.MIN_VALUE * tuple2.v1()));
  236. assertThat(multiplied.v2(), is(Float.MAX_VALUE * tuple2.v2()));
  237. }
  238. @Test
  239. public void testConvertionToIntTuple2() {
  240. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  241. final IntTuple2 converted = tuple2.asIntTuple2();
  242. assertThat(tuple2.v1(), is(converted.v1()));
  243. assertThat(tuple2.v2(), is(converted.v2()));
  244. assertThat(converted.v1(), is(this.v1));
  245. assertThat(converted.v2(), is(this.v2));
  246. }
  247. @Test
  248. public void testConvertionToDoubleTuple2() {
  249. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  250. final DoubleTuple2 converted = tuple2.asDoubleTuple2();
  251. assertThat(tuple2.v1().doubleValue(), is(converted.v1()));
  252. assertThat(tuple2.v2().doubleValue(), is(converted.v2()));
  253. assertThat(converted.v1(), is((double) this.v1));
  254. assertThat(converted.v2(), is((double) this.v2));
  255. }
  256. @Test
  257. public void testConvertionToLongTuple2() {
  258. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  259. final LongTuple2 converted = tuple2.asLongTuple2();
  260. assertThat(tuple2.v1().longValue(), is(converted.v1()));
  261. assertThat(tuple2.v2().longValue(), is(converted.v2()));
  262. assertThat(converted.v1(), is((long) this.v1));
  263. assertThat(converted.v2(), is((long) this.v2));
  264. }
  265. @Test
  266. public void testConvertionToFloatTuple2() {
  267. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  268. final FloatTuple2 converted = tuple2.asFloatTuple2();
  269. assertThat(tuple2.v1().floatValue(), is(converted.v1()));
  270. assertThat(tuple2.v2().floatValue(), is(converted.v2()));
  271. assertThat(converted.v1(), is((float) this.v1));
  272. assertThat(converted.v2(), is((float) this.v2));
  273. }
  274. @Test
  275. public void testLongValues() {
  276. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  277. assertThat(tuple2.v1Long(), is((long) this.v1));
  278. assertThat(tuple2.v2Long(), is((long) this.v2));
  279. }
  280. @Test
  281. public void testIntValues() {
  282. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  283. assertThat(tuple2.v1Int(), is(this.v1));
  284. assertThat(tuple2.v2Int(), is(this.v2));
  285. }
  286. @Test
  287. public void testValues() {
  288. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  289. assertThat(tuple2.v1(), is(this.v1));
  290. assertThat(tuple2.v2(), is(this.v2));
  291. }
  292. @Test
  293. public void testDoubleValues() {
  294. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  295. assertThat(tuple2.v1Double(), is((double) this.v1));
  296. assertThat(tuple2.v2Double(), is((double) this.v2));
  297. }
  298. @Test
  299. public void testFloatValues() {
  300. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  301. assertThat(tuple2.v1Float(), is((float) this.v1));
  302. assertThat(tuple2.v2Float(), is((float) this.v2));
  303. }
  304. @Test
  305. public void testAsIntArray() {
  306. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  307. final int[] intArray = tuple2.asIntArray();
  308. assertThat(intArray.length, is(2));
  309. assertThat(intArray[0], is(this.v1));
  310. assertThat(intArray[1], is(this.v2));
  311. }
  312. @Test
  313. public void testAsArray() {
  314. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  315. final Object[] array = tuple2.asArray();
  316. assertThat(array.length, is(2));
  317. assertThat(array[0], is(this.v1));
  318. assertThat(array[1], is(this.v2));
  319. }
  320. @Test
  321. public void testAsList() {
  322. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  323. final List<Object> array = tuple2.asList();
  324. assertThat(array.size(), is(2));
  325. assertThat(array.get(0), is(this.v1));
  326. assertThat(array.get(1), is(this.v2));
  327. }
  328. @Test
  329. public void testDimension() {
  330. final IntTuple2 tuple2 = Tuples.of(this.v1, this.v2);
  331. assertThat(tuple2.dimension(), is(2));
  332. }
  333. }