|
1 | 1 | import * as array from './array.js' |
2 | 2 | import * as t from './testing.js' |
| 3 | +import * as prng from './prng.js' |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * @param {t.TestCase} _tc |
@@ -117,3 +118,29 @@ export const testUnique = _tc => { |
117 | 118 | t.compare([{ el: 1 }], array.uniqueBy([{ el: 1 }, { el: 1 }], o => o.el)) |
118 | 119 | t.compare([], array.uniqueBy([], o => o)) |
119 | 120 | } |
| 121 | + |
| 122 | +/** |
| 123 | + * @param {t.TestCase} tc |
| 124 | + */ |
| 125 | +export const testRepeatBubblesortItem = tc => { |
| 126 | + const arr = Array.from(prng.uint8Array(tc.prng, 10)) |
| 127 | + arr.sort((a, b) => a - b) |
| 128 | + const newItem = prng.uint32(tc.prng, 0, 256) |
| 129 | + const pos = prng.uint32(tc.prng, 0, arr.length) |
| 130 | + arr.splice(pos, newItem) |
| 131 | + const arrCopySorted = arr.slice().sort((a, b) => a - b) |
| 132 | + array.bubblesortItem(arr, pos, (a, b) => a - b) |
| 133 | + t.compare(arr, arrCopySorted) |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * @param {t.TestCase} tc |
| 138 | + */ |
| 139 | +export const testRepeatBubblesort = tc => { |
| 140 | + const arr = Array.from(prng.uint8Array(tc.prng, 10)) |
| 141 | + const arrCopySorted = arr.slice().sort((a, b) => a - b) |
| 142 | + for (let i = arr.length - 1; i >= 0; i--) { |
| 143 | + while (array.bubblesortItem(arr, i, (a, b) => a - b) !== i) { /* nop */ } |
| 144 | + } |
| 145 | + t.compare(arr, arrCopySorted) |
| 146 | +} |
0 commit comments