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

BookstoreTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import static org.junit.Assert.assertEquals;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import org.junit.Ignore;
  7. import org.junit.Test;
  8. public class BookstoreTest {
  9. // This is sufficient accuracy since we're handling currency values, which should be equal to within 2 decimal places.
  10. private static final double EQUALITY_TOLERANCE = 0.001;
  11. @Test
  12. public void onlyASingleBook() {
  13. List<Integer> books = new ArrayList<>(Collections.singletonList(1));
  14. Bookstore bookstore = new Bookstore(books);
  15. assertEquals(8, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  16. }
  17. @Ignore("Remove to run test")
  18. @Test
  19. public void twoOfSameBook() {
  20. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1));
  21. Bookstore bookstore = new Bookstore(books);
  22. assertEquals(16, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  23. }
  24. @Ignore("Remove to run test")
  25. @Test
  26. public void emptyBasket() {
  27. List<Integer> books = new ArrayList<>();
  28. Bookstore bookstore = new Bookstore(books);
  29. assertEquals(0, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  30. }
  31. @Ignore("Remove to run test")
  32. @Test
  33. public void twoDifferentBooks() {
  34. List<Integer> books = new ArrayList<>(Arrays.asList(1, 2));
  35. Bookstore bookstore = new Bookstore(books);
  36. assertEquals(15.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  37. }
  38. @Ignore("Remove to run test")
  39. @Test
  40. public void threeDifferentBooks() {
  41. List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3));
  42. Bookstore bookstore = new Bookstore(books);
  43. assertEquals(21.6, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  44. }
  45. @Ignore("Remove to run test")
  46. @Test
  47. public void fourDifferentBooks() {
  48. List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
  49. Bookstore bookstore = new Bookstore(books);
  50. assertEquals(25.6, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  51. }
  52. @Ignore("Remove to run test")
  53. @Test
  54. public void fiveDifferentBooks() {
  55. List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
  56. Bookstore bookstore = new Bookstore(books);
  57. assertEquals(30, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  58. }
  59. @Ignore("Remove to run test")
  60. @Test
  61. public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree() {
  62. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5));
  63. Bookstore bookstore = new Bookstore(books);
  64. assertEquals(51.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  65. }
  66. @Ignore("Remove to run test")
  67. @Test
  68. public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree() {
  69. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 4));
  70. Bookstore bookstore = new Bookstore(books);
  71. assertEquals(40.8, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  72. }
  73. @Ignore("Remove to run test")
  74. @Test
  75. public void twoEachOfFirst4BooksAnd1CopyEachOfRest() {
  76. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5));
  77. Bookstore bookstore = new Bookstore(books);
  78. assertEquals(55.60, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  79. }
  80. @Ignore("Remove to run test")
  81. @Test
  82. public void twoCopiesOfEachBook() {
  83. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));
  84. Bookstore bookstore = new Bookstore(books);
  85. assertEquals(60.00, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  86. }
  87. @Ignore("Remove to run test")
  88. @Test
  89. public void threeCopiesOfFirstBookAnd2EachOfRemaining() {
  90. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1));
  91. Bookstore bookstore = new Bookstore(books);
  92. assertEquals(68.00, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  93. }
  94. @Ignore("Remove to run test")
  95. @Test
  96. public void threeEachOFirst2BooksAnd2EachOfRemainingBooks() {
  97. List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2));
  98. Bookstore bookstore = new Bookstore(books);
  99. assertEquals(75.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
  100. }
  101. }