Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
thecrypticace committed Feb 10, 2025
commit af41deec781d606bc83c0879386ca6ae19361729
42 changes: 42 additions & 0 deletions packages/tailwindcss/src/utils/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ it.each([
['2', '1', GREATER],
['1', '10', LESS],
['10', '1', GREATER],

// Numbers of different lengths
['75', '700', LESS],
['700', '75', GREATER],
['75', '770', LESS],
['770', '75', GREATER],
])('should compare "%s" with "%s" as "%d"', (a, b, expected) => {
expect(Math.sign(compare(a, b))).toBe(expected)
})
Expand Down Expand Up @@ -124,3 +130,39 @@ it('should sort strings with multiple numbers consistently using the `compare` f
]
`)
})

it('sort is stable', () => {
// Heap's algorithm for permutations
function* permutations<T>(input: T[]) {
let pos = 1
let stack = input.map(() => 0)

yield input.slice()

while (pos < input.length) {
if (stack[pos] < pos) {
let k = pos % 2 == 0 ? 0 : stack[pos]
;[input[k], input[pos]] = [input[pos], input[k]]
yield input.slice()
++stack[pos]
pos = 1
} else {
stack[pos] = 0
++pos
}
}
}

let classes = ['duration-initial', 'duration-75', 'duration-150', 'duration-700', 'duration-1000']

for (let permutation of permutations(classes)) {
let sorted = [...permutation].sort(compare)
expect(sorted).toEqual([
'duration-75',
'duration-150',
'duration-700',
'duration-1000',
'duration-initial',
])
}
})