Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29e6ba8
commit 0faeda2
Showing
3 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { assert } from 'chai'; | ||
|
||
import { isSorted } from '../../util/isSorted'; | ||
|
||
describe('isSorted', () => { | ||
it('returns true for empty lists', () => { | ||
assert.isTrue(isSorted([])); | ||
}); | ||
|
||
it('returns true for one-element lists', () => { | ||
assert.isTrue(isSorted([5])); | ||
}); | ||
|
||
it('returns true for sorted lists', () => { | ||
assert.isTrue(isSorted([1, 2])); | ||
assert.isTrue(isSorted([1, 2, 2, 3])); | ||
}); | ||
|
||
it('returns false for out-of-order lists', () => { | ||
assert.isFalse(isSorted([2, 1])); | ||
assert.isFalse(isSorted([1, 2, 2, 3, 0])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
export function isSorted(list: Iterable<number>): boolean { | ||
let previousItem: undefined | number; | ||
for (const item of list) { | ||
if (previousItem !== undefined && item < previousItem) { | ||
return false; | ||
} | ||
previousItem = item; | ||
} | ||
return true; | ||
} |