|
| 1 | +import ImmutablePriorityQueue from "immutablepriorityqueue" |
| 2 | +import List from "list" |
| 3 | +import Array from "array" |
| 4 | + |
| 5 | +// formatter-ignore |
| 6 | +let lotsOfVals = [> |
| 7 | + 3, 2, 1, 5, 3, 2, 2, 10, 6, 5, |
| 8 | + 6, 4, 9, 10, 10, 5, 9, 6, 1, 3, |
| 9 | + 3, 6, 6, 1, 3, 9, 6, 7, 4, 6, |
| 10 | + 5, 4, 4, 8, 6, 4, 1, 2, 3, 8, |
| 11 | + 8, 8, 10, 9, 6, 8, 4, 1, 1, 2, |
| 12 | + 10, 2, 8, 10, 8, 7, 4, 8, 10, 1, |
| 13 | + 3, 10, 4, 1, 6, 9, 6, 7, 8, 7, |
| 14 | + 10, 4, 5, 10, 3, 2, 4, 5, 9, 5, |
| 15 | + 3, 5, 10, 8, 7, 1, 6, 1, 4, 8, |
| 16 | + 3, 5, 7, 2, 4, 2, 4, 5, 10, 6, |
| 17 | + 7, 7, 7, 1, 10, 4, 9, 9, 10, 10, |
| 18 | + 2, 5, 2, 4, 3, 10, 9, 8, 3, 5, |
| 19 | + 7, 10, 9, 7, 8, 9, 3, 7, 7, 1, |
| 20 | + 7, 7, 6, 3, 3, 5, 1, 8, 9, 10, |
| 21 | + 7, 5, 9, 2, 9, 1, 6, 3, 10, 6, |
| 22 | + 10, 2, 6, 2, 9, 3, 3, 2, 4, 1, |
| 23 | + 9, 4, 9, 2, 9, 4, 8, 5, 6, 7, |
| 24 | + 4, 6, 5, 4, 3, 4, 2, 9, 10, 1, |
| 25 | + 10, 4, 4, 6, 3, 1, 9, 10, 2, 2, |
| 26 | + 5, 5, 3, 4, 5, 8, 3, 8, 4, 10 |
| 27 | +] |
| 28 | +let numVals = Array.length(lotsOfVals) |
| 29 | + |
| 30 | +let sortedVals = Array.copy(lotsOfVals) |
| 31 | +Array.sort(compare, sortedVals) |
| 32 | + |
| 33 | +let mut pq = ImmutablePriorityQueue.make((a, b) => a - b) |
| 34 | +let mut maxPq = ImmutablePriorityQueue.make((a, b) => b - a) |
| 35 | + |
| 36 | +assert ImmutablePriorityQueue.size(pq) == 0 |
| 37 | +assert ImmutablePriorityQueue.isEmpty(pq) |
| 38 | +assert ImmutablePriorityQueue.peek(pq) == None |
| 39 | +assert ImmutablePriorityQueue.drain(pq) == [] |
| 40 | + |
| 41 | +pq = ImmutablePriorityQueue.pop(pq) |
| 42 | +assert ImmutablePriorityQueue.size(pq) == 0 |
| 43 | + |
| 44 | +for (let mut i = 0; i < numVals; i += 1) { |
| 45 | + let newPq = ImmutablePriorityQueue.push(lotsOfVals[i], pq) |
| 46 | + let newMaxPq = ImmutablePriorityQueue.push(lotsOfVals[i], maxPq) |
| 47 | + assert ImmutablePriorityQueue.size(pq) == i |
| 48 | + assert ImmutablePriorityQueue.size(maxPq) == i |
| 49 | + assert ImmutablePriorityQueue.size(newPq) == i + 1 |
| 50 | + assert ImmutablePriorityQueue.size(newMaxPq) == i + 1 |
| 51 | + pq = newPq |
| 52 | + maxPq = newMaxPq |
| 53 | +} |
| 54 | + |
| 55 | +let pqWithAll = pq |
| 56 | +let maxPqWithAll = maxPq |
| 57 | + |
| 58 | +for (let mut i = 0; i < numVals; i += 1) { |
| 59 | + let val = ImmutablePriorityQueue.peek(pq) |
| 60 | + assert val == Some(sortedVals[i]) |
| 61 | + let maxVal = ImmutablePriorityQueue.peek(maxPq) |
| 62 | + assert maxVal == Some(sortedVals[numVals - i - 1]) |
| 63 | + |
| 64 | + let newPq = ImmutablePriorityQueue.pop(pq) |
| 65 | + let newMaxPq = ImmutablePriorityQueue.pop(maxPq) |
| 66 | + let expectedSize = numVals - i |
| 67 | + assert ImmutablePriorityQueue.size(pq) == expectedSize |
| 68 | + assert ImmutablePriorityQueue.size(maxPq) == expectedSize |
| 69 | + assert ImmutablePriorityQueue.size(newPq) == expectedSize - 1 |
| 70 | + assert ImmutablePriorityQueue.size(newMaxPq) == expectedSize - 1 |
| 71 | + pq = newPq |
| 72 | + maxPq = newMaxPq |
| 73 | +} |
| 74 | + |
| 75 | +assert ImmutablePriorityQueue.size(pq) == 0 |
| 76 | +assert ImmutablePriorityQueue.size(pqWithAll) == numVals |
| 77 | +assert !ImmutablePriorityQueue.isEmpty(pqWithAll) |
| 78 | +assert ImmutablePriorityQueue.peek(pq) == None |
| 79 | + |
| 80 | +let sortedList = Array.toList(sortedVals) |
| 81 | +assert ImmutablePriorityQueue.drain(pqWithAll) == sortedList |
| 82 | +assert ImmutablePriorityQueue.drain(maxPqWithAll) == List.reverse(sortedList) |
| 83 | +assert ImmutablePriorityQueue.drain( |
| 84 | + ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b) |
| 85 | +) == |
| 86 | +sortedList |
| 87 | +assert ImmutablePriorityQueue.drain( |
| 88 | + ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a) |
| 89 | +) == |
| 90 | +List.reverse(sortedList) |
0 commit comments