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

BookStoreTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import static org.junit.Assert.assertEquals;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Arrays;
  5. import org.junit.Before;
  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. private BookStore bookStore;
  12. @Before
  13. public void setUp() {
  14. bookStore = new BookStore();
  15. }
  16. @Test
  17. public void onlyASingleBook() {
  18. List<Integer> books = Collections.singletonList(1);
  19. assertEquals(8.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  20. }
  21. @Ignore("Remove to run test")
  22. @Test
  23. public void twoOfSameBook() {
  24. List<Integer> books = Arrays.asList(2, 2);
  25. assertEquals(16.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  26. }
  27. @Ignore("Remove to run test")
  28. @Test
  29. public void emptyBasket() {
  30. List<Integer> books = Collections.emptyList();
  31. assertEquals(0.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  32. }
  33. @Ignore("Remove to run test")
  34. @Test
  35. public void twoDifferentBooks() {
  36. List<Integer> books = Arrays.asList(1, 2);
  37. assertEquals(15.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  38. }
  39. @Ignore("Remove to run test")
  40. @Test
  41. public void threeDifferentBooks() {
  42. List<Integer> books = Arrays.asList(1, 2, 3);
  43. assertEquals(21.60, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  44. }
  45. @Ignore("Remove to run test")
  46. @Test
  47. public void fourDifferentBooks() {
  48. List<Integer> books = Arrays.asList(1, 2, 3, 4);
  49. assertEquals(25.60, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  50. }
  51. @Ignore("Remove to run test")
  52. @Test
  53. public void fiveDifferentBooks() {
  54. List<Integer> books = Arrays.asList(1, 2, 3, 4, 5);
  55. assertEquals(30.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  56. }
  57. @Ignore("Remove to run test")
  58. @Test
  59. public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree() {
  60. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5);
  61. assertEquals(51.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  62. }
  63. @Ignore("Remove to run test")
  64. @Test
  65. public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree() {
  66. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 4);
  67. assertEquals(40.80, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  68. }
  69. @Ignore("Remove to run test")
  70. @Test
  71. public void twoEachOfFirst4BooksAnd1CopyEachOfRest() {
  72. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5);
  73. assertEquals(55.60, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  74. }
  75. @Ignore("Remove to run test")
  76. @Test
  77. public void twoCopiesOfEachBook() {
  78. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5);
  79. assertEquals(60.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  80. }
  81. @Ignore("Remove to run test")
  82. @Test
  83. public void threeCopiesOfFirstBookAnd2EachOfRemaining() {
  84. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1);
  85. assertEquals(68.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  86. }
  87. @Ignore("Remove to run test")
  88. @Test
  89. public void threeEachOFirst2BooksAnd2EachOfRemainingBooks() {
  90. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2);
  91. assertEquals(75.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  92. }
  93. @Ignore("Remove to run test")
  94. @Test
  95. public void fourGroupsOfFourAreCheaperThanTwoGroupsEachOfFiveAndThree() {
  96. List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5);
  97. assertEquals(102.4, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
  98. }
  99. }