Guest User

Untitled

a guest
Nov 6th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. let expect = require("chai").expect;
  2.  
  3. class SortedList {
  4. constructor() {
  5. this.list = [];
  6. }
  7.  
  8. add(element) {
  9. this.list.push(element);
  10. this.sort();
  11. }
  12.  
  13. remove(index) {
  14. this.vrfyRange(index);
  15. this.list.splice(index, 1);
  16. }
  17.  
  18. get(index) {
  19. this.vrfyRange(index);
  20. return this.list[index];
  21. }
  22.  
  23. vrfyRange(index) {
  24. if (this.list.length == 0) throw new Error("Collection is empty.");
  25. if (index < 0 || index >= this.list.length) throw new Error("Index was outside the bounds of the collection.");
  26. }
  27.  
  28. sort() {
  29. this.list.sort((a, b) => a - b);
  30. }
  31.  
  32. get size() {
  33. return this.list.length;
  34. }
  35. }
  36.  
  37.  
  38. describe("Tests for this task", function () {
  39.  
  40. let myList;
  41. beforeEach(function () {
  42. myList = new SortedList();
  43. });
  44. //Test initial state:
  45. describe("Test initial state", function () {
  46. it("add should exist", function () {
  47. expect(SortedList.prototype.hasOwnProperty("add")).to.equal(true);
  48. });
  49. it("remove should exist", function () {
  50. expect(SortedList.prototype.hasOwnProperty("remove")).to.equal(true);
  51. });
  52. it("get should exist", function () {
  53. expect(SortedList.prototype.hasOwnProperty("get")).to.equal(true);
  54. });
  55. it("size should exist", function () {
  56. expect(SortedList.prototype.hasOwnProperty("size")).to.equal(true);
  57. })
  58. });
  59.  
  60. //add():
  61. describe("Test add", function () {
  62. it("add with 1 element", function () {
  63. myList.add(5);
  64. expect(myList.list.join(", ")).to.equal("5");
  65. });
  66. it("add with many elements", function () {
  67. myList.add(5);
  68. myList.add(4);
  69. myList.add(3);
  70. expect(myList.list.join(", ")).to.equal("3, 4, 5");
  71. });
  72. });
  73.  
  74. //remove():
  75. describe("Test remove", function () {
  76. it("remove with empty list", function () {
  77. expect(() => myList.remove()).throw(Error, "Collection is empty.")
  78. });
  79. it("remove with negative index", function () {
  80. myList.add(3);
  81. expect(() => myList.remove(-1)).throw(Error, "Index was outside the bounds of the collection.")
  82. });
  83. it("remove - index is equal to list length", function () {
  84. myList.add(3);
  85. expect(() => myList.remove(1)).throw(Error, "Index was outside the bounds of the collection.")
  86. });
  87. it("remove - index is bigger than list length", function () {
  88. myList.add(3);
  89. expect(() => myList.remove(2)).throw(Error, "Index was outside the bounds of the collection.")
  90. });
  91. it("remove - test with correct index", function () {
  92. myList.add(3);
  93. myList.add(4);
  94. myList.add(5);
  95. myList.remove(1);
  96. expect(myList.list.join(", ")).to.equal("3, 5");
  97. });
  98. });
  99.  
  100. //get():
  101. describe("Test get", function () {
  102. it("get with empty list", function () {
  103. expect(() => myList.get()).throw(Error, "Collection is empty.")
  104. });
  105. it("get with negative index", function () {
  106. myList.add(3);
  107. expect(() => myList.get(-1)).throw(Error, "Index was outside the bounds of the collection.")
  108. });
  109. it("get - index is equal to list length", function () {
  110. myList.add(3);
  111. expect(() => myList.get(1)).throw(Error, "Index was outside the bounds of the collection.")
  112. });
  113. it("get - index is bigger than list length", function () {
  114. myList.add(3);
  115. expect(() => myList.get(2)).throw(Error, "Index was outside the bounds of the collection.")
  116. });
  117. it("get - test with correct index", function () {
  118. myList.add(3);
  119. myList.add(4);
  120. myList.add(5);
  121. let element = myList.get(1);
  122. expect(element).to.equal(4);
  123. });
  124. });
  125.  
  126. //size():
  127. describe("Test size", function () {
  128. it("size with empty list", function () {
  129. expect(myList.size).to.equal(0);
  130. });
  131. it("size with non-empty list", function () {
  132. myList.add(1);
  133. myList.add(2);
  134. myList.add(3);
  135. expect(myList.size).to.equal(3);
  136. });
  137. });
  138. });
Advertisement
Add Comment
Please, Sign In to add comment