Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
ab193fe4
Commit
ab193fe4
authored
Jun 04, 2010
by
Pieter Noordhuis
Browse files
generated tests for different encodings to avoid test code duplication
parent
d4507ec6
Changes
1
Hide whitespace changes
Inline
Side-by-side
tests/unit/type/list.tcl
View file @
ab193fe4
...
@@ -64,56 +64,58 @@ start_server {
...
@@ -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 c
heck_numbered_list_consistency
{
key
}
{
proc c
reate_list
{
key entries
}
{
set len
[
r llen
$key
]
r del
$key
for
{
set i 0
}
{
$i
< $len
}
{
incr i
}
{
r rpush $key
"aaaaaaaaaaaaaaaaa"
assert_equal $i
[
r lindex $key $i
]
foreach entry $entries
{
r rpush $key $entry
}
assert_equal
[
expr $len-1-$i
]
[
r lindex $key
[
expr
(
-$i
)
-1
]
]
assert_equal
"aaaaaaaaaaaaaaaaa"
[
r lpop $key
]
}
assert_encoding list $key
}
}
proc check_random_access_consistency
{
key
}
{
foreach
{
type num
}
{
ziplist 250 list 500
}
{
set len
[
r llen $key
]
proc check_numbered_list_consistency
{
key
}
{
for
{
set i 0
}
{
$i
< $len
}
{
incr i
}
{
set len
[
r llen $key
]
set rint
[
expr int
(
rand
()
*$len
)]
for
{
set i 0
}
{
$i
< $len
}
{
incr i
}
{
assert_equal $rint
[
r lindex $key $rint
]
assert_equal $i
[
r lindex $key $i
]
assert_equal
[
expr $len-1-$rint
]
[
r lindex $key
[
expr
(
-$rint
)
-1
]]
assert_equal
[
expr $len-1-$i
]
[
r lindex $key
[
expr
(
-$i
)
-1
]]
}
}
}
}
test
{
LINDEX consistency test - ziplist
}
{
proc check_random_access_consistency
{
key
}
{
populate_with_numbers myziplist 250
set len
[
r llen $key
]
assert_encoding ziplist myziplist
for
{
set i 0
}
{
$i
< $len
}
{
incr i
}
{
}
set rint
[
expr int
(
rand
()
*$len
)]
assert_equal $rint
[
r lindex $key $rint
]
test
{
LINDEX consistency test - regular list
}
{
assert_equal
[
expr $len-1-$rint
]
[
r lindex $key
[
expr
(
-$rint
)
-1
]]
populate_with_numbers mylist 500
}
assert_encoding list mylist
}
check_numbered_list_consistency mylist
}
test
{
LINDEX random access - ziplist
}
{
test
"LINDEX consistency test -
$type
"
{
assert_encoding ziplist myziplist
r del mylist
check_random_access_consistency myziplist
for
{
set i 0
}
{
$i
< $num
}
{
incr i
}
{
}
r rpush mylist $i
}
assert_encoding $type mylist
check_numbered_list_consistency mylist
}
test
{
LINDEX random access -
regular list
}
{
test
"
LINDEX random access -
$type
"
{
assert_encoding
list
mylist
assert_encoding
$type
mylist
check_random_access_consistency mylist
check_random_access_consistency mylist
}
}
test
{
Check if
both list encodings are
still ok after a DEBUG RELOAD
}
{
test
"
Check if
list is
still ok after a DEBUG RELOAD
-
$type
"
{
r debug reload
r debug reload
assert_encoding
ziplist myzip
list
assert_encoding
$type my
list
check_numbered_list_consistency my
zip
list
check_numbered_list_consistency mylist
assert_encoding list
mylist
check_random_access_consistency
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 {
...
@@ -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
]
proc create_regular_list
{
key entries
}
{
assert_equal
{
a b
}
[
r lrange mylist1 0 -1
]
r del $key
assert_equal
{
c d
}
[
r lrange mylist2 0 -1
]
r rpush $key
"aaaaaaaaaaaaaaaaa"
assert_encoding ziplist mylist2
foreach entry $entries
{
r rpush $key $entry
}
}
assert_equal
"aaaaaaaaaaaaaaaaa"
[
r lpop $key
]
assert_match *encoding:list*
[
r debug object $key
]
}
test
{
RPOPLPUSH base case - ziplist
}
{
r del mylist1 mylist2
create_ziplist 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 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
}
{
test
"RPOPLPUSH with the same list as src and dst -
$type
"
{
create_regular_list srclist
{
a b c d
}
create_$type mylist
{
a b c
}
create_ziplist dstlist
{
x
}
assert_equal
{
a b c
}
[
r lrange mylist 0 -1
]
assert_equal d
[
r rpoplpush srclist dstlist
]
assert_equal c
[
r rpoplpush mylist mylist
]
assert_equal c
[
r rpoplpush srclist dstlist
]
assert_equal
{
c a b
}
[
r lrange mylist 0 -1
]
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
}
{
foreach othertype
{
ziplist list
}
{
create_regular_list srclist
{
a b c d
}
test
"RPOPLPUSH with
$type
source and existing target
$othertype
"
{
create_regular_list dstlist
{
x
}
create_$type srclist
{
a b c d
}
assert_equal d
[
r rpoplpush srclist dstlist
]
create_$othertype dstlist
{
x
}
assert_equal c
[
r rpoplpush srclist dstlist
]
assert_equal d
[
r rpoplpush srclist dstlist
]
assert_equal
{
a b
}
[
r lrange srclist 0 -1
]
assert_equal c
[
r rpoplpush srclist dstlist
]
assert_equal
{
c d x
}
[
r lrange dstlist 0 -1
]
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 {
...
@@ -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
assert_equal
{}
[
r lpop myziplist
]
# pop on empty list
assert_equal
{}
[
r rpop myziplist
]
assert_equal
{}
[
r lpop mylist
]
}
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 {
...
@@ -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
}
{
incr sum2
[
r lpop mylist
]
incr sum2
[
r rpop mylist
]
}
assert_equal $sum1 $sum2
}
}
test
{
Mass RPOP/LPOP - regular
list
}
{
foreach type
{
ziplist
list
}
{
r del mylist
test
"LRANGE basics -
$type
"
{
set sum1 0
create_$type mylist
{
0 1 2 3 4 5 6 7 8 9
}
for
{
set i 0
}
{
$i
< 500
}
{
incr i
}
{
assert_equal
{
1 2 3 4 5 6 7 8
}
[
r lrange mylist 1 -2
]
r lpush
mylist
$i
assert_equal
{
7 8 9
}
[
r lrange
mylist
-3 -1
]
incr sum1 $i
assert_equal
{
4
}
[
r lrange mylist 4 4
]
}
}
assert_encoding list mylist
set sum2 0
for
{
set i 0
}
{
$i
< 250
}
{
incr i
}
{
incr sum2
[
r lpop mylist
]
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
}
{
test
"
LRANGE
inverted indexes -
$type
"
{
create_
ziplist
mylist
{
1 2 3
}
create_
$type
mylist
{
0
1 2 3
4 5 6 7 8 9
}
assert_equal
{
1 2 3
}
[
r lrange mylist
-1000 1000
]
assert_equal
{}
[
r lrange mylist
6 2
]
}
}
test
{
LRANGE out of range indexes including the full list - regular list
}
{
test
"LRANGE out of range indexes including the full list -
$type
"
{
create_regular_list mylist
{
1 2 3
}
create_$type mylist
{
1 2 3
}
assert_equal
{
1 2 3
}
[
r lrange mylist -1000 1000
]
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 stress testing -
$type
"
{
test
{
LTRIM basics - regular list
}
{
set mylist
{}
r del mylist
for
{
set i 0
}
{
$i
< 20
}
{
incr i
}
{
r lpush mylist
"aaaaaaaaaaaaaaaaa"
lappend mylist $i
assert_encoding list mylist
}
for
{
set i 0
}
{
$i
< 100
}
{
incr i
}
{
r lpush mylist $i
for
{
set j 0
}
{
$j
< 100
}
{
incr j
}
{
r ltrim mylist 0 4
create_$type 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
]
}
}
}
r lrange mylist 0 -1
}
{
99 98 97 96 95
}
test
{
LTRIM stress testing - ziplist
}
{
set mylist
{}
for
{
set i 0
}
{
$i
< 20
}
{
incr i
}
{
lappend mylist $i
}
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 {
...
@@ -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
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment