Advertisement
UniQuet0p1

Untitled

Nov 4th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1.  
  2. import static org.junit.Assert.*;
  3. import org.junit.Test;
  4. import java.util.*;
  5.  
  6. /** Testklass.
  7. * @author Jaanus
  8. */
  9. public class LfractionTest {
  10.  
  11. @Test (timeout=1000)
  12. public void testPlus() {
  13. Lfraction f1 = new Lfraction (2, 5);
  14. Lfraction f2 = new Lfraction (4, 15);
  15. Lfraction sum = f1.plus(f2);
  16. assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
  17. new Lfraction (2, 3), sum);
  18. Lfraction sm2 = sum.plus (sum);
  19. assertEquals ("Wrong sum.plus(sum): <2/3> + <2/3>",
  20. new Lfraction (4, 3), sm2);
  21. assertEquals ("Do not change the arguments of the sum",
  22. new Lfraction (2, 3), sum);
  23. f1 = new Lfraction (-1, 250);
  24. f2 = new Lfraction (-1, 375);
  25. sum = f1.plus(f2);
  26. assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
  27. new Lfraction (-1, 150), sum);
  28. f1 = new Lfraction (1, 221);
  29. f2 = new Lfraction (1, 323);
  30. sum = f1.plus (f2);
  31. assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
  32. new Lfraction (32, 4199), sum);
  33. f1 = new Lfraction (1, 39203);
  34. f2 = new Lfraction (1, 41989);
  35. sum = f1.plus (f2);
  36. assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
  37. new Lfraction (408, 8271833), sum);
  38. f1 = new Lfraction (-2, 5);
  39. f2 = new Lfraction (2, 5);
  40. sum = f1.plus(f2);
  41. assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">",
  42. new Lfraction (0, 1), sum);
  43. }
  44.  
  45. @Test (timeout=1000)
  46. public void testTimes() {
  47. Lfraction f1 = new Lfraction (2, 5);
  48. Lfraction f2 = new Lfraction (4, 15);
  49. Lfraction prd = f1.times (f2);
  50. assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
  51. new Lfraction (8, 75), prd);
  52. f1 = new Lfraction (-3, 5);
  53. f2 = new Lfraction (-5, 7);
  54. prd = f1.times (f2);
  55. assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
  56. new Lfraction (3, 7), prd);
  57. Lfraction pr2 = prd.times (prd);
  58. assertEquals ("Wrong prd.times(prd): <3/7> * <3/7>",
  59. new Lfraction (9, 49), pr2);
  60. assertEquals ("Do not change the arguments of the product",
  61. new Lfraction (3, 7), prd);
  62. f1 = new Lfraction (0, 1);
  63. f2 = new Lfraction (2, 3);
  64. prd = f1.times (f2);
  65. assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
  66. new Lfraction (0, 1), prd);
  67. f1 = new Lfraction (3, 2);
  68. f2 = new Lfraction (2, 3);
  69. prd = f1.times (f2);
  70. assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">",
  71. new Lfraction (1, 1), prd);
  72. f1 = new Lfraction (3, 5);
  73. f2 = new Lfraction (5, 7);
  74. prd = f1.times (f2);
  75. assertTrue ("Result must be reduced: 3/5 * 5/7 -> 3/7",
  76. prd.getDenominator()==7);
  77. }
  78.  
  79. @Test (expected=RuntimeException.class)
  80. public void testCreateZeroDenominator() {
  81. Lfraction f = new Lfraction (1, 0);
  82. }
  83.  
  84. @Test (expected=RuntimeException.class)
  85. public void testZeroInverse() {
  86. Lfraction f = new Lfraction (0, 1);
  87. f.inverse();
  88. }
  89.  
  90. @Test (timeout=1000)
  91. public void testClone() {
  92. Lfraction f1 = new Lfraction (2, 5);
  93. Lfraction f2 = null;
  94. try {
  95. f2 = (Lfraction)f1.clone();
  96. } catch (CloneNotSupportedException e) {};
  97. assertNotSame ("clone must differ from original", f2, f1);
  98. assertEquals ("clone must be equal to original", f1, f2);
  99. f1 = f2.plus(f1);
  100. assertEquals ("clone must be independent from original",
  101. new Lfraction (2, 5), f2);
  102. }
  103.  
  104. @Test (timeout=1000)
  105. public void testToString() {
  106. String s = new Lfraction (1, 4).toString();
  107. assertTrue (s + " must represent quarter",
  108. (s.indexOf('1') < s.indexOf('4')) && (s.indexOf('1') >= 0));
  109. s = new Lfraction (-1, 5).toString();
  110. assertTrue (s + " does not contain minus", s.indexOf('-') >= 0);
  111. }
  112.  
  113. @Test (expected=RuntimeException.class)
  114. public void testDivideByZero() {
  115. Lfraction f1 = new Lfraction (1, 5);
  116. Lfraction f2 = new Lfraction (0, 1);
  117. Lfraction q = f1.divideBy (f2);
  118. }
  119.  
  120. @Test (timeout=1000)
  121. public void testMinus() {
  122. Lfraction f1 = new Lfraction (2, 5);
  123. Lfraction f2 = new Lfraction (4, 15);
  124. Lfraction dif = f1.minus (f2);
  125. assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">",
  126. new Lfraction (2, 15), dif);
  127. Lfraction df2 = dif.minus (dif);
  128. assertEquals ("Wrong difference: <2/15> - <2/15>",
  129. new Lfraction (0, 1), df2);
  130. assertEquals ("Do not change the arguments of the difference",
  131. new Lfraction (2, 15), dif);
  132. f1 = new Lfraction (-2, 5);
  133. f2 = new Lfraction (-4, 15);
  134. dif = f1.minus (f2);
  135. assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">",
  136. new Lfraction (-2, 15), dif);
  137. }
  138.  
  139. @Test (timeout=1000)
  140. public void testDivideBy() {
  141. Lfraction f1 = new Lfraction (-2, 7);
  142. Lfraction f2 = new Lfraction (-1, 14);
  143. Lfraction f = f1.divideBy (f2);
  144. assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">",
  145. new Lfraction (4, 1), f);
  146. f = f2.divideBy (f1);
  147. assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">",
  148. new Lfraction (1, 4), f);
  149. Lfraction f3 = f.divideBy (f);
  150. assertEquals (f.toString() + " divided by itself",
  151. new Lfraction (1, 1), f3);
  152. assertEquals ("Do not change the arguments of the quotient",
  153. new Lfraction (1, 4), f);
  154. }
  155.  
  156. @Test (timeout=1000)
  157. public void testOpposite() {
  158. Lfraction f1 = new Lfraction (1, 6);
  159. Lfraction f2 = f1.opposite();
  160. assertEquals ("Wrong opposite", new Lfraction (-1, 6), f2);
  161. assertEquals ("Do not change the argument of opposite",
  162. new Lfraction (1, 6), f1);
  163. f1 = new Lfraction (-4, 75);
  164. f2 = f1.opposite();
  165. assertEquals ("Wrong opposite", new Lfraction (4, 75), f2);
  166. f1 = new Lfraction (0, 1);
  167. f2 = f1.opposite();
  168. assertEquals ("zero must be neutral to opposite", f1, f2);
  169. }
  170.  
  171. @Test (timeout=1000)
  172. public void testInverse() {
  173. Lfraction f1 = new Lfraction (2, 3);
  174. Lfraction f2 = f1.inverse();
  175. assertEquals ("Wrong inverse ", new Lfraction (3, 2), f2);
  176. assertEquals ("Do not change the argument of inverse",
  177. new Lfraction (2, 3), f1);
  178. f1 = new Lfraction (-4, 75);
  179. f2 = f1.inverse();
  180. assertEquals ("Wrong inverse", new Lfraction (-75, 4), f2);
  181. assertTrue ("Denominator must always be positive",
  182. f2.getDenominator() > 0);
  183. f1 = new Lfraction (1, 1);
  184. f2 = f1.inverse();
  185. assertEquals ("1 must be neutral to inverse", f1, f2);
  186. }
  187.  
  188. @Test (timeout=1000)
  189. public void testGetters() {
  190. Lfraction f1 = new Lfraction (2, 3);
  191. long num = f1.getNumerator();
  192. assertEquals ("wrong numerator ", 2, num);
  193. f1 = new Lfraction (-4, 75);
  194. num = f1.getNumerator();
  195. assertEquals ("Wrong numerator", -4, num);
  196. f1 = new Lfraction (0, 7);
  197. num = f1.getNumerator();
  198. assertEquals ("Wrong numerator", 0, num);
  199. f1 = new Lfraction (2, 3);
  200. long den = f1.getDenominator();
  201. assertEquals ("wrong denominator ", 3, den);
  202. f1 = new Lfraction (-4, 75);
  203. den = f1.getDenominator();
  204. assertEquals ("Wrong denominator", 75, den);
  205. }
  206.  
  207. @Test (timeout=1000)
  208. public void testIntegerPart() {
  209. Lfraction f1 = new Lfraction (2, 3);
  210. long i = f1.integerPart();
  211. assertEquals ("wrong integer part ", 0, i);
  212. f1 = new Lfraction (3, 2);
  213. i = f1.integerPart();
  214. assertEquals ("wrong integer part ", 1, i);
  215. f1 = new Lfraction (32, 3);
  216. i = f1.integerPart();
  217. assertEquals ("wrong integer part ", 10, i);
  218. f1 = new Lfraction (33, 3);
  219. i = f1.integerPart();
  220. assertEquals ("wrong integer part ", 11, i);
  221. f1 = new Lfraction (-33, 3);
  222. i = f1.integerPart();
  223. assertEquals ("wrong integer part ", -11, i);
  224. }
  225.  
  226. @Test (timeout=1000)
  227. public void testLfractionPart() {
  228. Lfraction f1 = new Lfraction (2, 3);
  229. Lfraction i = f1.fractionPart();
  230. assertEquals ("wrong fraction part ", new Lfraction (2, 3), i);
  231. f1 = new Lfraction (3, 2);
  232. i = f1.fractionPart();
  233. assertEquals ("wrong fraction part ", new Lfraction (1, 2), i);
  234. f1 = new Lfraction (32, 3);
  235. i = f1.fractionPart();
  236. assertEquals ("wrong fraction part ", new Lfraction (2, 3), i);
  237. f1 = new Lfraction (33, 3);
  238. i = f1.fractionPart();
  239. assertEquals ("wrong fraction part ", new Lfraction (0, 1), i);
  240. f1 = new Lfraction (-33, 3);
  241. i = f1.fractionPart();
  242. assertEquals ("wrong fraction part ", new Lfraction (0, 1), i);
  243. f1 = new Lfraction (-5, 4);
  244. i = f1.fractionPart();
  245. assertTrue ("wrong fraction part " + i.toString()
  246. + " for " + f1.toString(),
  247. i.equals (new Lfraction (-1, 4)) || i.equals (new Lfraction (3, 4)));
  248. }
  249.  
  250. @Test (timeout=1000)
  251. public void testEquals() {
  252. Lfraction f1 = new Lfraction (2, 5);
  253. Lfraction f2 = new Lfraction (4, 10);
  254. assertTrue ("2/5 must be equal to 4/10", f1.equals (f2));
  255. assertFalse ("2/5 is not 3/5", f1.equals (new Lfraction (3, 5)));
  256. f1 = new Lfraction (12345678901234567L, 1L);
  257. f2 = new Lfraction (12345678901234568L, 1L);
  258. assertFalse ("12345678901234567/1 is not 12345678901234568/1",
  259. f1.equals (f2));
  260. }
  261.  
  262. @Test (timeout=1000)
  263. public void testCompareTo() {
  264. Lfraction f1 = new Lfraction (2, 5);
  265. Lfraction f2 = new Lfraction (4, 7);
  266. assertTrue ("2/5 must be less than 4/7", f1.compareTo (f2) < 0);
  267. assertTrue ("2/5 must be equal to 4/10",
  268. f1.compareTo (new Lfraction (4, 10)) == 0);
  269. assertTrue ("4/7 must be greater than 2/5", f2.compareTo (f1) > 0);
  270. f1 = new Lfraction (-2, 5);
  271. f2 = new Lfraction (-4, 7);
  272. assertTrue ("-2/5 must be greater than -4/7", f1.compareTo (f2) > 0);
  273. assertTrue ("-2/5 must be equal to -4/10",
  274. f1.compareTo (new Lfraction (-4, 10)) == 0);
  275. assertTrue ("-4/7 must be less than -2/5", f2.compareTo (f1) < 0);
  276. f1 = new Lfraction (12345678901234567L, 1L);
  277. f2 = new Lfraction (12345678901234568L, 1L);
  278. assertFalse ("12345678901234567/1 is not 12345678901234568/1",
  279. f1.compareTo (f2) == 0);
  280. }
  281.  
  282. @Test (timeout=1000)
  283. public void testToLfraction() {
  284. Lfraction f1 = Lfraction.toLfraction (Math.PI, 7);
  285. Lfraction f2 = new Lfraction (22, 7);
  286. assertTrue ("Math.PI must be nearly 22/7", f1.equals (f2));
  287. f1 = Lfraction.toLfraction (-10., 2);
  288. f2 = new Lfraction (-20, 2);
  289. assertTrue ("-10. must be -20/2", f1.equals (f2));
  290. }
  291.  
  292. @Test (timeout=1000)
  293. public void testToDouble() {
  294. Lfraction f = new Lfraction (2, 5);
  295. assertEquals ("2/5 must be nearly 0.4", 0.4, f.toDouble(), 0.00001);
  296. f = new Lfraction (-17, 100);
  297. assertEquals ("-17/100 must be nearly -0.17", -0.17,
  298. f.toDouble(), 0.00001);
  299. }
  300.  
  301. @Test (timeout=1000)
  302. public void testValueOf() {
  303. Lfraction f = new Lfraction (2, 5);
  304. assertEquals ("valueOf must read back what toString outputs. ",
  305. f, Lfraction.valueOf (f.toString()));
  306. f = new Lfraction (-17, 100);
  307. assertEquals ("valueOf must read back what toString outputs. ",
  308. f, Lfraction.valueOf (f.toString()));
  309. }
  310.  
  311. @Test (timeout=1000)
  312. public void testHashCode() {
  313. Lfraction q1 = new Lfraction (1L, 2L);
  314. int h1 = q1.hashCode();
  315. Lfraction q2 = new Lfraction (1L, 2L);
  316. int h2 = q2.hashCode();
  317. Lfraction q3 = null;
  318. try {
  319. q3 = (Lfraction)q1.clone();
  320. } catch (CloneNotSupportedException e) {};
  321. int h3 = q3.hashCode();
  322. assertTrue ("hashCode has to be same for equal objects", h1 == h2);
  323. assertTrue ("hashCode has to be same for clone objects", h1 == h3);
  324. assertTrue ("hashCode has to be same for the same object",
  325. h1 == q1.hashCode());
  326. q2 = new Lfraction (0L, 2L);
  327. h2 = q2.hashCode();
  328. q3 = new Lfraction (1L, 3L);
  329. h3 = q3.hashCode();
  330. assertFalse ("hashCode does not depend on numerator", h1 == h2);
  331. assertFalse ("hashCode does not depend on denominator", h1 == h3);
  332. }
  333. @Test (timeout=1000)
  334. public void testPow0() {
  335. Lfraction f1 = new Lfraction (-2, 5);
  336. System.out.print(new Lfraction (1, 1));
  337. }
  338. @Test (timeout=1000)
  339. public void testPow1() {
  340. Lfraction f1 = new Lfraction (2, 5);
  341. System.out.print(new Lfraction (2, 5));
  342. }
  343. @Test (timeout=1000)
  344. public void testPow_1() {
  345. Lfraction f1 = new Lfraction (2, 5);
  346. System.out.print(new Lfraction (5, 2));
  347. }
  348. @Test (timeout=1000)
  349. public void testPowN() {
  350. Lfraction f1 = new Lfraction (2, 5);
  351. System.out.print(new Lfraction (4, 25));
  352. }
  353.  
  354. @Test (timeout=1000)
  355. public void testPowN_() {
  356. Lfraction f1 = new Lfraction (-2, 5);
  357. System.out.print(new Lfraction (4, 25));
  358. }
  359. }
  360.  
  361.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement