Last active
July 16, 2016 14:32
-
-
Save brezniczky/49a214d678dded13b0fde80d851e004f to your computer and use it in GitHub Desktop.
Test for the is_ordered.R gist.
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
# Verification of the is_ordered.R solution. | |
library(testthat) | |
test.generate.random.sequences = function() { | |
# generate some random sequence | |
# | |
# first: a random walk with a drift of unit increases or stagnation | |
n = 1000 | |
root.sequence = c(1, 1 + cumsum(sample(c(0, 1), n, replace = TRUE))) | |
root.range = range(root.sequence) | |
# make it still ordered, but no longer increasing | |
transform.map = sample(1:max(root.sequence)) | |
sorted.seq = transform.map[root.sequence] | |
# also create a flawed series: duplicate a value | |
unsorted.seq = | |
sample(c(min(root.sequence):(max(root.sequence) - 1), | |
root.sequence[n %/% 2])) | |
return(list(sorted = sorted.seq, unsorted = unsorted.seq)) | |
} | |
test = function() { | |
# demo tests | |
# be reproducible/re-runnable | |
set.seed(1000) | |
# easy-to-debug test cases | |
expect_true(is.ordered.sequence(1)) | |
expect_true(is.ordered.sequence(1:2)) | |
expect_false(is.ordered.sequence(c(1, 3, 1))) | |
expect_true(is.ordered.sequence(1:4)) | |
expect_true(is.ordered.sequence(rep(1:4, each = 3))) | |
# tests with more sizeable and trickier input | |
gen.seq.list = test.generate.random.sequences() | |
expect_true(is.ordered.sequence(gen.seq.list$sorted)) | |
expect_false(is.ordered.sequence(gen.seq.list$unsorted)) | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment