generated tests for different encodings to avoid test code duplication

This commit is contained in:
Pieter Noordhuis 2010-06-04 17:05:54 +02:00
parent d4507ec615
commit ab193fe452

View File

@ -64,56 +64,58 @@ start_server {
assert_equal 0 [r llen mylist2] assert_equal 0 [r llen mylist2]
} }
proc populate_with_numbers {key num} { proc create_ziplist {key entries} {
for {set i 0} {$i < $num} {incr i} { r del $key
r rpush $key $i foreach entry $entries { r rpush $key $entry }
assert_encoding ziplist $key
}
proc create_list {key entries} {
r del $key
r rpush $key "aaaaaaaaaaaaaaaaa"
foreach entry $entries { r rpush $key $entry }
assert_equal "aaaaaaaaaaaaaaaaa" [r lpop $key]
assert_encoding list $key
}
foreach {type num} {ziplist 250 list 500} {
proc check_numbered_list_consistency {key} {
set len [r llen $key]
for {set i 0} {$i < $len} {incr i} {
assert_equal $i [r lindex $key $i]
assert_equal [expr $len-1-$i] [r lindex $key [expr (-$i)-1]]
}
} }
}
proc check_numbered_list_consistency {key} { proc check_random_access_consistency {key} {
set len [r llen $key] set len [r llen $key]
for {set i 0} {$i < $len} {incr i} { for {set i 0} {$i < $len} {incr i} {
assert_equal $i [r lindex $key $i] set rint [expr int(rand()*$len)]
assert_equal [expr $len-1-$i] [r lindex $key [expr (-$i)-1]] assert_equal $rint [r lindex $key $rint]
assert_equal [expr $len-1-$rint] [r lindex $key [expr (-$rint)-1]]
}
} }
}
proc check_random_access_consistency {key} { test "LINDEX consistency test - $type" {
set len [r llen $key] r del mylist
for {set i 0} {$i < $len} {incr i} { for {set i 0} {$i < $num} {incr i} {
set rint [expr int(rand()*$len)] r rpush mylist $i
assert_equal $rint [r lindex $key $rint] }
assert_equal [expr $len-1-$rint] [r lindex $key [expr (-$rint)-1]] assert_encoding $type mylist
check_numbered_list_consistency mylist
} }
}
test {LINDEX consistency test - ziplist} { test "LINDEX random access - $type" {
populate_with_numbers myziplist 250 assert_encoding $type mylist
assert_encoding ziplist myziplist check_random_access_consistency mylist
} }
test {LINDEX consistency test - regular list} { test "Check if list is still ok after a DEBUG RELOAD - $type" {
populate_with_numbers mylist 500 r debug reload
assert_encoding list mylist assert_encoding $type mylist
check_numbered_list_consistency mylist check_numbered_list_consistency mylist
} check_random_access_consistency mylist
}
test {LINDEX random access - ziplist} {
assert_encoding ziplist myziplist
check_random_access_consistency myziplist
}
test {LINDEX random access - regular list} {
assert_encoding list mylist
check_random_access_consistency mylist
}
test {Check if both list encodings are still ok after a DEBUG RELOAD} {
r debug reload
assert_encoding ziplist myziplist
check_numbered_list_consistency myziplist
assert_encoding list mylist
check_numbered_list_consistency mylist
} }
test {LLEN against non-list value error} { test {LLEN against non-list value error} {
@ -142,70 +144,34 @@ start_server {
assert_error ERR* {r rpush mylist 0} assert_error ERR* {r rpush mylist 0}
} }
proc create_ziplist {key entries} { foreach type {ziplist list} {
r del $key test "RPOPLPUSH base case - $type" {
foreach entry $entries { r rpush $key $entry } r del mylist1 mylist2
assert_match *encoding:ziplist* [r debug object $key] create_$type mylist1 {a b c d}
} assert_equal d [r rpoplpush mylist1 mylist2]
assert_equal c [r rpoplpush mylist1 mylist2]
assert_equal {a b} [r lrange mylist1 0 -1]
assert_equal {c d} [r lrange mylist2 0 -1]
assert_encoding ziplist mylist2
}
proc create_regular_list {key entries} { test "RPOPLPUSH with the same list as src and dst - $type" {
r del $key create_$type mylist {a b c}
r rpush $key "aaaaaaaaaaaaaaaaa" assert_equal {a b c} [r lrange mylist 0 -1]
foreach entry $entries { r rpush $key $entry } assert_equal c [r rpoplpush mylist mylist]
assert_equal "aaaaaaaaaaaaaaaaa" [r lpop $key] assert_equal {c a b} [r lrange mylist 0 -1]
assert_match *encoding:list* [r debug object $key] }
}
test {RPOPLPUSH base case - ziplist} { foreach othertype {ziplist list} {
r del mylist1 mylist2 test "RPOPLPUSH with $type source and existing target $othertype" {
create_ziplist mylist1 {a b c d} create_$type srclist {a b c d}
assert_equal d [r rpoplpush mylist1 mylist2] create_$othertype dstlist {x}
assert_equal c [r rpoplpush mylist1 mylist2] assert_equal d [r rpoplpush srclist dstlist]
assert_equal {a b} [r lrange mylist1 0 -1] assert_equal c [r rpoplpush srclist dstlist]
assert_equal {c d} [r lrange mylist2 0 -1] assert_equal {a b} [r lrange srclist 0 -1]
assert_encoding ziplist mylist2 assert_equal {c d x} [r lrange dstlist 0 -1]
} }
}
test {RPOPLPUSH base case - regular list} {
r del mylist1 mylist2
create_regular_list mylist1 {a b c d}
assert_equal d [r rpoplpush mylist1 mylist2]
assert_equal c [r rpoplpush mylist1 mylist2]
assert_equal {a b} [r lrange mylist1 0 -1]
assert_equal {c d} [r lrange mylist2 0 -1]
assert_encoding ziplist mylist2
}
test {RPOPLPUSH with the same list as src and dst - ziplist} {
create_ziplist myziplist {a b c}
assert_equal {a b c} [r lrange myziplist 0 -1]
assert_equal c [r rpoplpush myziplist myziplist]
assert_equal {c a b} [r lrange myziplist 0 -1]
}
test {RPOPLPUSH with the same list as src and dst - regular list} {
create_regular_list mylist {a b c}
assert_equal {a b c} [r lrange mylist 0 -1]
assert_equal c [r rpoplpush mylist mylist]
assert_equal {c a b} [r lrange mylist 0 -1]
}
test {RPOPLPUSH target list already exists, being a ziplist} {
create_regular_list srclist {a b c d}
create_ziplist dstlist {x}
assert_equal d [r rpoplpush srclist dstlist]
assert_equal c [r rpoplpush srclist dstlist]
assert_equal {a b} [r lrange srclist 0 -1]
assert_equal {c d x} [r lrange dstlist 0 -1]
}
test {RPOPLPUSH target list already exists, being a regular list} {
create_regular_list srclist {a b c d}
create_regular_list dstlist {x}
assert_equal d [r rpoplpush srclist dstlist]
assert_equal c [r rpoplpush srclist dstlist]
assert_equal {a b} [r lrange srclist 0 -1]
assert_equal {c d x} [r lrange dstlist 0 -1]
} }
test {RPOPLPUSH against non existing key} { test {RPOPLPUSH against non existing key} {
@ -236,28 +202,18 @@ start_server {
assert_equal {} [r rpoplpush srclist dstlist] assert_equal {} [r rpoplpush srclist dstlist]
} {} } {}
test {Basic LPOP/RPOP - ziplist} { foreach type {ziplist list} {
create_ziplist myziplist {0 1 2} test "Basic LPOP/RPOP - $type" {
assert_equal 0 [r lpop myziplist] create_$type mylist {0 1 2}
assert_equal 2 [r rpop myziplist] assert_equal 0 [r lpop mylist]
assert_equal 1 [r lpop myziplist] assert_equal 2 [r rpop mylist]
assert_equal 0 [r llen myziplist] assert_equal 1 [r lpop mylist]
assert_equal 0 [r llen mylist]
# pop on empty list # pop on empty list
assert_equal {} [r lpop myziplist] assert_equal {} [r lpop mylist]
assert_equal {} [r rpop myziplist] assert_equal {} [r rpop mylist]
} }
test {Basic LPOP/RPOP - regular list} {
create_regular_list mylist {0 1 2}
assert_equal 0 [r lpop mylist]
assert_equal 2 [r rpop mylist]
assert_equal 1 [r lpop mylist]
assert_equal 0 [r llen mylist]
# pop on empty list
assert_equal {} [r lpop mylist]
assert_equal {} [r rpop mylist]
} }
test {LPOP/RPOP against non list value} { test {LPOP/RPOP against non list value} {
@ -266,152 +222,89 @@ start_server {
assert_error ERR*kind* {r rpop notalist} assert_error ERR*kind* {r rpop notalist}
} }
test {Mass RPOP/LPOP - ziplist} { foreach {type num} {ziplist 250 list 500} {
r del mylist test "Mass RPOP/LPOP - $type" {
set sum1 0 r del mylist
for {set i 0} {$i < 250} {incr i} { set sum1 0
r lpush mylist $i for {set i 0} {$i < $num} {incr i} {
incr sum1 $i r lpush mylist $i
incr sum1 $i
}
assert_encoding $type mylist
set sum2 0
for {set i 0} {$i < [expr $num/2]} {incr i} {
incr sum2 [r lpop mylist]
incr sum2 [r rpop mylist]
}
assert_equal $sum1 $sum2
} }
assert_encoding ziplist mylist }
set sum2 0
for {set i 0} {$i < 125} {incr i} { foreach type {ziplist list} {
incr sum2 [r lpop mylist] test "LRANGE basics - $type" {
incr sum2 [r rpop mylist] create_$type mylist {0 1 2 3 4 5 6 7 8 9}
assert_equal {1 2 3 4 5 6 7 8} [r lrange mylist 1 -2]
assert_equal {7 8 9} [r lrange mylist -3 -1]
assert_equal {4} [r lrange mylist 4 4]
} }
assert_equal $sum1 $sum2
}
test {Mass RPOP/LPOP - regular list} { test "LRANGE inverted indexes - $type" {
r del mylist create_$type mylist {0 1 2 3 4 5 6 7 8 9}
set sum1 0 assert_equal {} [r lrange mylist 6 2]
for {set i 0} {$i < 500} {incr i} {
r lpush mylist $i
incr sum1 $i
} }
assert_encoding list mylist
set sum2 0 test "LRANGE out of range indexes including the full list - $type" {
for {set i 0} {$i < 250} {incr i} { create_$type mylist {1 2 3}
incr sum2 [r lpop mylist] assert_equal {1 2 3} [r lrange mylist -1000 1000]
incr sum2 [r rpop mylist]
} }
assert_equal $sum1 $sum2
}
test {LRANGE basics - ziplist} {
create_ziplist mylist {0 1 2 3 4 5 6 7 8 9}
assert_equal {1 2 3 4 5 6 7 8} [r lrange mylist 1 -2]
assert_equal {7 8 9} [r lrange mylist -3 -1]
assert_equal {4} [r lrange mylist 4 4]
}
test {LRANGE basics - ziplist} {
create_regular_list mylist {0 1 2 3 4 5 6 7 8 9}
assert_equal {1 2 3 4 5 6 7 8} [r lrange mylist 1 -2]
assert_equal {7 8 9} [r lrange mylist -3 -1]
assert_equal {4} [r lrange mylist 4 4]
}
test {LRANGE inverted indexes - ziplist} {
create_ziplist mylist {0 1 2 3 4 5 6 7 8 9}
assert_equal {} [r lrange mylist 6 2]
}
test {LRANGE inverted indexes - regular list} {
create_regular_list mylist {0 1 2 3 4 5 6 7 8 9}
assert_equal {} [r lrange mylist 6 2]
}
test {LRANGE out of range indexes including the full list - ziplist} {
create_ziplist mylist {1 2 3}
assert_equal {1 2 3} [r lrange mylist -1000 1000]
}
test {LRANGE out of range indexes including the full list - regular list} {
create_regular_list mylist {1 2 3}
assert_equal {1 2 3} [r lrange mylist -1000 1000]
} }
test {LRANGE against non existing key} { test {LRANGE against non existing key} {
assert_equal {} [r lrange nosuchkey 0 1] assert_equal {} [r lrange nosuchkey 0 1]
} }
test {LTRIM basics - ziplist} { foreach type {ziplist list} {
r del mylist test "LTRIM basics - $type" {
r lpush mylist "aaa" create_$type mylist "foo"
assert_encoding ziplist mylist for {set i 0} {$i < 100} {incr i} {
for {set i 0} {$i < 100} {incr i} { r lpush mylist $i
r lpush mylist $i r ltrim mylist 0 4
r ltrim mylist 0 4 }
} r lrange mylist 0 -1
r lrange mylist 0 -1 } {99 98 97 96 95}
} {99 98 97 96 95}
test {LTRIM basics - regular list} { test "LTRIM stress testing - $type" {
r del mylist set mylist {}
r lpush mylist "aaaaaaaaaaaaaaaaa" for {set i 0} {$i < 20} {incr i} {
assert_encoding list mylist lappend mylist $i
for {set i 0} {$i < 100} {incr i} { }
r lpush mylist $i
r ltrim mylist 0 4
}
r lrange mylist 0 -1
} {99 98 97 96 95}
test {LTRIM stress testing - ziplist} { for {set j 0} {$j < 100} {incr j} {
set mylist {} create_$type mylist $mylist
for {set i 0} {$i < 20} {incr i} {
lappend mylist $i # Trim at random
set a [randomInt 20]
set b [randomInt 20]
r ltrim mylist $a $b
assert_equal [lrange $mylist $a $b] [r lrange mylist 0 -1]
}
} }
for {set j 0} {$j < 100} {incr j} {
create_ziplist mylist $mylist
# Trim at random
set a [randomInt 20]
set b [randomInt 20]
r ltrim mylist $a $b
assert_equal [lrange $mylist $a $b] [r lrange mylist 0 -1]
}
} }
test {LTRIM stress testing - regular list} { foreach type {ziplist list} {
set mylist {} test "LSET - $type" {
for {set i 0} {$i < 20} {incr i} { create_$type mylist {99 98 97 96 95}
lappend mylist $i r lset mylist 1 foo
r lset mylist -1 bar
assert_equal {99 foo 97 96 bar} [r lrange mylist 0 -1]
} }
for {set j 0} {$j < 100} {incr j} { test "LSET out of range index - $type" {
create_regular_list mylist $mylist assert_error ERR*range* {r lset mylist 10 foo}
# Trim at random
set a [randomInt 20]
set b [randomInt 20]
r ltrim mylist $a $b
assert_equal [lrange $mylist $a $b] [r lrange mylist 0 -1]
} }
} }
test {LSET - ziplist} {
create_ziplist mylist {99 98 97 96 95}
r lset mylist 1 foo
r lset mylist -1 bar
assert_equal {99 foo 97 96 bar} [r lrange mylist 0 -1]
}
test {LSET out of range index - ziplist} {
assert_error ERR*range* {r lset mylist 10 foo}
}
test {LSET - regular list} {
create_regular_list mylist {99 98 97 96 95}
r lset mylist 1 foo
r lset mylist -1 bar
assert_equal {99 foo 97 96 bar} [r lrange mylist 0 -1]
}
test {LSET out of range index - regular list} {
assert_error ERR*range* {r lset mylist 10 foo}
}
test {LSET against non existing key} { test {LSET against non existing key} {
assert_error ERR*key* {r lset nosuchkey 10 foo} assert_error ERR*key* {r lset nosuchkey 10 foo}
} }
@ -421,37 +314,35 @@ start_server {
assert_error ERR*value* {r lset nolist 0 foo} assert_error ERR*value* {r lset nolist 0 foo}
} }
foreach type {ziplist regular_list} { foreach type {ziplist list} {
set formatted_type [string map {"_" " "} $type] test "LREM remove all the occurrences - $type" {
test "LREM remove all the occurrences - $formatted_type" {
create_$type mylist {foo bar foobar foobared zap bar test foo} create_$type mylist {foo bar foobar foobared zap bar test foo}
assert_equal 2 [r lrem mylist 0 bar] assert_equal 2 [r lrem mylist 0 bar]
assert_equal {foo foobar foobared zap test foo} [r lrange mylist 0 -1] assert_equal {foo foobar foobared zap test foo} [r lrange mylist 0 -1]
} }
test "LREM remove the first occurrence - $formatted_type" { test "LREM remove the first occurrence - $type" {
assert_equal 1 [r lrem mylist 1 foo] assert_equal 1 [r lrem mylist 1 foo]
assert_equal {foobar foobared zap test foo} [r lrange mylist 0 -1] assert_equal {foobar foobared zap test foo} [r lrange mylist 0 -1]
} }
test "LREM remove non existing element - $formatted_type" { test "LREM remove non existing element - $type" {
assert_equal 0 [r lrem mylist 1 nosuchelement] assert_equal 0 [r lrem mylist 1 nosuchelement]
assert_equal {foobar foobared zap test foo} [r lrange mylist 0 -1] assert_equal {foobar foobared zap test foo} [r lrange mylist 0 -1]
} }
test "LREM starting from tail with negative count - $formatted_type" { test "LREM starting from tail with negative count - $type" {
create_$type mylist {foo bar foobar foobared zap bar test foo foo} create_$type mylist {foo bar foobar foobared zap bar test foo foo}
assert_equal 1 [r lrem mylist -1 bar] assert_equal 1 [r lrem mylist -1 bar]
assert_equal {foo bar foobar foobared zap test foo foo} [r lrange mylist 0 -1] assert_equal {foo bar foobar foobared zap test foo foo} [r lrange mylist 0 -1]
} }
test "LREM starting from tail with negative count (2) - $formatted_type" { test "LREM starting from tail with negative count (2) - $type" {
assert_equal 2 [r lrem mylist -2 foo] assert_equal 2 [r lrem mylist -2 foo]
assert_equal {foo bar foobar foobared zap test} [r lrange mylist 0 -1] assert_equal {foo bar foobar foobared zap test} [r lrange mylist 0 -1]
} }
test "LREM deleting objects that may be int encoded - $formatted_type" { test "LREM deleting objects that may be int encoded - $type" {
create_$type myotherlist {1 2 3} create_$type myotherlist {1 2 3}
assert_equal 1 [r lrem myotherlist 1 2] assert_equal 1 [r lrem myotherlist 1 2]
assert_equal 2 [r llen myotherlist] assert_equal 2 [r llen myotherlist]