test.js 8.6 KB

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