Skip to content

Instantly share code, notes, and snippets.

@AbuCarlo
Created December 27, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbuCarlo/fc14dd3e8859c774e7f6984b8b678004 to your computer and use it in GitHub Desktop.
Save AbuCarlo/fc14dd3e8859c774e7f6984b8b678004 to your computer and use it in GitHub Desktop.
This Groovy script can be used to generate stress tests for the final week's assignment for the Coursera course `Basic Algorithms`
def UPPER_BOUND = 1000001 // SetWithRangeSums.MODULUS
def PROBLEM_SIZE = 100000
def javaSet = new SetWithRangeSums.SummingTreeSet()
def avlSet = new SetWithRangeSums.PrincetonAvlTree()
/* The inputs from the assignment are about 45 % insertion, 45 % queries, 5 % deletion,
and 5% range sums.
*/
println(PROBLEM_SIZE)
def random = new Random()
for (int i = 0; i < PROBLEM_SIZE; ++i) {
def operation = random.nextDouble()
if (operation < 0.35) {
int value = random.nextInt(UPPER_BOUND)
println "+ $value"
javaSet.add(value)
avlSet.add(value)
} else if (operation < 0.70) {
int value = random.nextInt(UPPER_BOUND)
println "? $value"
boolean javaContains = javaSet.contains(value)
boolean avlContains = avlSet.contains(value)
assert javaContains == avlContains
} else if (operation < 0.85) {
int value = random.nextInt(UPPER_BOUND)
println "- $value"
javaSet.remove(value)
avlSet.remove(value)
} else {
int from = random.nextInt(UPPER_BOUND)
int to = random.nextInt(UPPER_BOUND)
println "s $from $to"
long javaSum = javaSet.rangeSum(from, to)
long avlSum = avlSet.rangeSum(from, to)
assert javaSum == avlSum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment