test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. var tape = require('tape')
  2. var sorted = require('./')
  3. tape('add', function (t) {
  4. var list = []
  5. sorted.add(list, 3)
  6. sorted.add(list, 4)
  7. sorted.add(list, 3)
  8. sorted.add(list, 9)
  9. sorted.add(list, 0)
  10. sorted.add(list, 5)
  11. sorted.add(list, 8)
  12. t.same(list, [0, 3, 3, 4, 5, 8, 9])
  13. t.end()
  14. })
  15. tape('addFromFront', function (t) {
  16. var list = []
  17. sorted.addFromFront(list, 3)
  18. sorted.addFromFront(list, 4)
  19. sorted.addFromFront(list, 3)
  20. sorted.addFromFront(list, 9)
  21. sorted.addFromFront(list, 0)
  22. sorted.addFromFront(list, 5)
  23. sorted.addFromFront(list, 8)
  24. t.same(list, [0, 3, 3, 4, 5, 8, 9])
  25. t.end()
  26. })
  27. tape('remove', function (t) {
  28. var list = []
  29. sorted.add(list, 3)
  30. sorted.add(list, 4)
  31. sorted.add(list, 3)
  32. sorted.add(list, 9)
  33. sorted.add(list, 0)
  34. sorted.add(list, 5)
  35. sorted.add(list, 8)
  36. sorted.remove(list, 3)
  37. sorted.remove(list, 5)
  38. sorted.remove(list, 6)
  39. t.same(list, [0, 3, 4, 8, 9])
  40. t.end()
  41. })
  42. tape('has', function (t) {
  43. var list = []
  44. sorted.add(list, 3)
  45. t.same(sorted.has(list, 3), true)
  46. t.same(sorted.has(list, 2), false)
  47. sorted.add(list, 5)
  48. t.same(sorted.has(list, 5), true)
  49. t.same(sorted.has(list, 3), true)
  50. t.same(sorted.has(list, 2), false)
  51. sorted.add(list, 1)
  52. t.same(sorted.has(list, 1), true)
  53. t.same(sorted.has(list, 5), true)
  54. t.same(sorted.has(list, 3), true)
  55. t.same(sorted.has(list, 2), false)
  56. t.same(sorted.has(list, 8), false)
  57. t.end()
  58. })
  59. tape('eq', function (t) {
  60. var list = []
  61. sorted.add(list, 3)
  62. t.same(sorted.eq(list, 3), 0)
  63. t.same(sorted.eq(list, 2), -1)
  64. sorted.add(list, 5)
  65. t.same(sorted.eq(list, 5), 1)
  66. t.same(sorted.eq(list, 3), 0)
  67. t.same(sorted.eq(list, 2), -1)
  68. sorted.add(list, 1)
  69. t.same(sorted.eq(list, 1), 0)
  70. t.same(sorted.eq(list, 5), 2)
  71. t.same(sorted.eq(list, 3), 1)
  72. t.same(sorted.eq(list, 2), -1)
  73. t.same(sorted.eq(list, 8), -1)
  74. t.end()
  75. })
  76. tape('gte', function (t) {
  77. var list = []
  78. sorted.add(list, 3)
  79. t.same(sorted.gte(list, 3), 0)
  80. t.same(sorted.gte(list, 2), 0)
  81. sorted.add(list, 5)
  82. t.same(sorted.gte(list, 5), 1)
  83. t.same(sorted.gte(list, 3), 0)
  84. t.same(sorted.gte(list, 2), 0)
  85. sorted.add(list, 1)
  86. t.same(sorted.gte(list, 1), 0)
  87. t.same(sorted.gte(list, 5), 2)
  88. t.same(sorted.gte(list, 3), 1)
  89. t.same(sorted.gte(list, 2), 1)
  90. t.same(sorted.gte(list, 8), -1)
  91. t.end()
  92. })
  93. tape('gt', function (t) {
  94. var list = []
  95. sorted.add(list, 3)
  96. t.same(sorted.gt(list, 3), -1)
  97. t.same(sorted.gt(list, 2), 0)
  98. sorted.add(list, 5)
  99. t.same(sorted.gt(list, 5), -1)
  100. t.same(sorted.gt(list, 3), 1)
  101. t.same(sorted.gt(list, 2), 0)
  102. sorted.add(list, 1)
  103. t.same(sorted.gt(list, 1), 1)
  104. t.same(sorted.gt(list, 5), -1)
  105. t.same(sorted.gt(list, 3), 2)
  106. t.same(sorted.gt(list, 2), 1)
  107. t.same(sorted.gt(list, 8), -1)
  108. t.end()
  109. })
  110. tape('lte', function (t) {
  111. var list = []
  112. sorted.add(list, 3)
  113. t.same(sorted.lte(list, 3), 0)
  114. t.same(sorted.lte(list, 2), -1)
  115. sorted.add(list, 5)
  116. t.same(sorted.lte(list, 6), 1)
  117. t.same(sorted.lte(list, 5), 1)
  118. t.same(sorted.lte(list, 3), 0)
  119. t.same(sorted.lte(list, 2), -1)
  120. sorted.add(list, 1)
  121. t.same(sorted.lte(list, 1), 0)
  122. t.same(sorted.lte(list, 5), 2)
  123. t.same(sorted.lte(list, 3), 1)
  124. t.same(sorted.lte(list, 2), 0)
  125. t.same(sorted.lte(list, 8), 2)
  126. t.end()
  127. })
  128. tape('lt', function (t) {
  129. var list = []
  130. sorted.add(list, 3)
  131. t.same(sorted.lt(list, 3), -1)
  132. t.same(sorted.lt(list, 2), -1)
  133. t.same(sorted.lt(list, 4), 0)
  134. sorted.add(list, 5)
  135. t.same(sorted.lt(list, 6), 1)
  136. t.same(sorted.lt(list, 5), 0)
  137. t.same(sorted.lt(list, 3), -1)
  138. t.same(sorted.lt(list, 2), -1)
  139. sorted.add(list, 1)
  140. t.same(sorted.lt(list, 1), -1)
  141. t.same(sorted.lt(list, 5), 1)
  142. t.same(sorted.lt(list, 3), 0)
  143. t.same(sorted.lt(list, 2), 0)
  144. t.same(sorted.lt(list, 8), 2)
  145. t.end()
  146. })
  147. tape('custom compare add', function (t) {
  148. var list = []
  149. sorted.add(list, {foo: 3}, cmp)
  150. sorted.add(list, {foo: 4}, cmp)
  151. sorted.add(list, {foo: 3}, cmp)
  152. sorted.add(list, {foo: 9}, cmp)
  153. sorted.add(list, {foo: 0}, cmp)
  154. sorted.add(list, {foo: 5}, cmp)
  155. sorted.add(list, {foo: 8}, cmp)
  156. t.same(list, [{foo: 0}, {foo: 3}, {foo: 3}, {foo: 4}, {foo: 5}, {foo: 8}, {foo: 9}])
  157. t.end()
  158. })
  159. tape('custom compare remove', function (t) {
  160. var list = []
  161. sorted.add(list, {foo: 3}, cmp)
  162. sorted.add(list, {foo: 4}, cmp)
  163. sorted.add(list, {foo: 3}, cmp)
  164. sorted.add(list, {foo: 9}, cmp)
  165. sorted.add(list, {foo: 0}, cmp)
  166. sorted.add(list, {foo: 5}, cmp)
  167. sorted.add(list, {foo: 8}, cmp)
  168. sorted.remove(list, {foo: 3}, cmp)
  169. sorted.remove(list, {foo: 5}, cmp)
  170. sorted.remove(list, {foo: 6}, cmp)
  171. t.same(list, [{foo: 0}, {foo: 3}, {foo: 4}, {foo: 8}, {foo: 9}])
  172. t.end()
  173. })
  174. tape('custom compare has', function (t) {
  175. var list = []
  176. sorted.add(list, {foo: 3}, cmp)
  177. t.same(sorted.has(list, {foo: 3}, cmp), true)
  178. t.same(sorted.has(list, {foo: 2}, cmp), false)
  179. sorted.add(list, {foo: 5}, cmp)
  180. t.same(sorted.has(list, {foo: 5}, cmp), true)
  181. t.same(sorted.has(list, {foo: 3}, cmp), true)
  182. t.same(sorted.has(list, {foo: 2}, cmp), false)
  183. sorted.add(list, {foo: 1}, cmp)
  184. t.same(sorted.has(list, {foo: 1}, cmp), true)
  185. t.same(sorted.has(list, {foo: 5}, cmp), true)
  186. t.same(sorted.has(list, {foo: 3}, cmp), true)
  187. t.same(sorted.has(list, {foo: 2}, cmp), false)
  188. t.same(sorted.has(list, {foo: 8}, cmp), false)
  189. t.end()
  190. })
  191. tape('custom compare eq', function (t) {
  192. var list = []
  193. sorted.add(list, {foo: 3}, cmp)
  194. t.same(sorted.eq(list, {foo: 3}, cmp), 0)
  195. t.same(sorted.eq(list, {foo: 2}, cmp), -1)
  196. sorted.add(list, {foo: 5}, cmp)
  197. t.same(sorted.eq(list, {foo: 5}, cmp), 1)
  198. t.same(sorted.eq(list, {foo: 3}, cmp), 0)
  199. t.same(sorted.eq(list, {foo: 2}, cmp), -1)
  200. sorted.add(list, {foo: 1}, cmp)
  201. t.same(sorted.eq(list, {foo: 1}, cmp), 0)
  202. t.same(sorted.eq(list, {foo: 5}, cmp), 2)
  203. t.same(sorted.eq(list, {foo: 3}, cmp), 1)
  204. t.same(sorted.eq(list, {foo: 2}, cmp), -1)
  205. t.same(sorted.eq(list, {foo: 8}, cmp), -1)
  206. t.end()
  207. })
  208. tape('custom compare gte', function (t) {
  209. var list = []
  210. sorted.add(list, {foo: 3}, cmp)
  211. t.same(sorted.gte(list, {foo: 3}, cmp), 0)
  212. t.same(sorted.gte(list, {foo: 2}, cmp), 0)
  213. sorted.add(list, {foo: 5}, cmp)
  214. t.same(sorted.gte(list, {foo: 5}, cmp), 1)
  215. t.same(sorted.gte(list, {foo: 3}, cmp), 0)
  216. t.same(sorted.gte(list, {foo: 2}, cmp), 0)
  217. sorted.add(list, {foo: 1}, cmp)
  218. t.same(sorted.gte(list, {foo: 1}, cmp), 0)
  219. t.same(sorted.gte(list, {foo: 5}, cmp), 2)
  220. t.same(sorted.gte(list, {foo: 3}, cmp), 1)
  221. t.same(sorted.gte(list, {foo: 2}, cmp), 1)
  222. t.same(sorted.gte(list, {foo: 8}, cmp), -1)
  223. t.end()
  224. })
  225. tape('custom compare gt', function (t) {
  226. var list = []
  227. sorted.add(list, {foo: 3}, cmp)
  228. t.same(sorted.gt(list, {foo: 3}, cmp), -1)
  229. t.same(sorted.gt(list, {foo: 2}, cmp), 0)
  230. sorted.add(list, {foo: 5}, cmp)
  231. t.same(sorted.gt(list, {foo: 5}, cmp), -1)
  232. t.same(sorted.gt(list, {foo: 3}, cmp), 1)
  233. t.same(sorted.gt(list, {foo: 2}, cmp), 0)
  234. sorted.add(list, {foo: 1}, cmp)
  235. t.same(sorted.gt(list, {foo: 1}, cmp), 1)
  236. t.same(sorted.gt(list, {foo: 5}, cmp), -1)
  237. t.same(sorted.gt(list, {foo: 3}, cmp), 2)
  238. t.same(sorted.gt(list, {foo: 2}, cmp), 1)
  239. t.same(sorted.gt(list, {foo: 8}, cmp), -1)
  240. t.end()
  241. })
  242. tape('custom compare lte', function (t) {
  243. var list = []
  244. sorted.add(list, {foo: 3}, cmp)
  245. t.same(sorted.lte(list, {foo: 3}, cmp), 0)
  246. t.same(sorted.lte(list, {foo: 2}, cmp), -1)
  247. sorted.add(list, {foo: 5}, cmp)
  248. t.same(sorted.lte(list, {foo: 6}, cmp), 1)
  249. t.same(sorted.lte(list, {foo: 5}, cmp), 1)
  250. t.same(sorted.lte(list, {foo: 3}, cmp), 0)
  251. t.same(sorted.lte(list, {foo: 2}, cmp), -1)
  252. sorted.add(list, {foo: 1}, cmp)
  253. t.same(sorted.lte(list, {foo: 1}, cmp), 0)
  254. t.same(sorted.lte(list, {foo: 5}, cmp), 2)
  255. t.same(sorted.lte(list, {foo: 3}, cmp), 1)
  256. t.same(sorted.lte(list, {foo: 2}, cmp), 0)
  257. t.same(sorted.lte(list, {foo: 8}, cmp), 2)
  258. t.end()
  259. })
  260. tape('custom compare lt', function (t) {
  261. var list = []
  262. sorted.add(list, {foo: 3}, cmp)
  263. t.same(sorted.lt(list, {foo: 3}, cmp), -1)
  264. t.same(sorted.lt(list, {foo: 2}, cmp), -1)
  265. t.same(sorted.lt(list, {foo: 4}, cmp), 0)
  266. sorted.add(list, {foo: 5}, cmp)
  267. t.same(sorted.lt(list, {foo: 6}, cmp), 1)
  268. t.same(sorted.lt(list, {foo: 5}, cmp), 0)
  269. t.same(sorted.lt(list, {foo: 3}, cmp), -1)
  270. t.same(sorted.lt(list, {foo: 2}, cmp), -1)
  271. sorted.add(list, {foo: 1}, cmp)
  272. t.same(sorted.lt(list, {foo: 1}, cmp), -1)
  273. t.same(sorted.lt(list, {foo: 5}, cmp), 1)
  274. t.same(sorted.lt(list, {foo: 3}, cmp), 0)
  275. t.same(sorted.lt(list, {foo: 2}, cmp), 0)
  276. t.same(sorted.lt(list, {foo: 8}, cmp), 2)
  277. t.end()
  278. })
  279. tape('find nearest value', function (t) {
  280. var list = []
  281. sorted.add(list, 0.001)
  282. sorted.add(list, 10)
  283. sorted.add(list, 20)
  284. sorted.add(list, 30)
  285. sorted.add(list, 40)
  286. sorted.add(list, 50)
  287. sorted.add(list, 70)
  288. t.equal(sorted.nearest(list, 66), 6)
  289. t.equal(sorted.nearest(list, 51), 5)
  290. t.equal(sorted.nearest(list, 1), 0)
  291. t.equal(sorted.nearest(list, 0), 0)
  292. t.equal(sorted.nearest(list, 69.999), 6)
  293. t.equal(sorted.nearest(list, 72), 6)
  294. t.end()
  295. })
  296. function cmp (a, b) {
  297. return a.foo - b.foo
  298. }