From 0091125cae64d409f5f29a93076de065f1c8ff89 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 11 Jun 2020 12:38:51 +0200 Subject: [PATCH] LPOS: tests + crash fix. --- src/t_list.c | 2 +- tests/unit/type/list.tcl | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/t_list.c b/src/t_list.c index 653337d78..e580139ab 100644 --- a/src/t_list.c +++ b/src/t_list.c @@ -554,7 +554,7 @@ void lposCommand(client *c) { /* We return NULL or an empty array if there is no such key (or * if we find no matches, depending on the presence of the COUNT option. */ - if ((o = lookupKeyWriteOrReply(c,c->argv[1],NULL)) == NULL) { + if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) { if (count != -1) addReply(c,shared.emptyarray); else diff --git a/tests/unit/type/list.tcl b/tests/unit/type/list.tcl index 676896a75..a0c04dcaa 100644 --- a/tests/unit/type/list.tcl +++ b/tests/unit/type/list.tcl @@ -6,6 +6,50 @@ start_server { } { source "tests/unit/type/list-common.tcl" + test {LPOS basic usage} { + r DEL mylist + r RPUSH mylist a b c 1 2 3 c c + assert {[r LPOS mylist a] == 0} + assert {[r LPOS mylist c] == 2} + } + + test {LPOS FIRST (positive and negative rank) option} { + assert {[r LPOS mylist c FIRST 1] == 2} + assert {[r LPOS mylist c FIRST 2] == 6} + assert {[r LPOS mylist c FIRST 4] eq ""} + assert {[r LPOS mylist c FIRST -1] == 7} + assert {[r LPOS mylist c FIRST -2] == 6} + } + + test {LPOS COUNT option} { + assert {[r LPOS mylist c COUNT 0] == {2 6 7}} + assert {[r LPOS mylist c COUNT 1] == {2}} + assert {[r LPOS mylist c COUNT 2] == {2 6}} + assert {[r LPOS mylist c COUNT 100] == {2 6 7}} + } + + test {LPOS COUNT + FIRST option} { + assert {[r LPOS mylist c COUNT 0 FIRST 2] == {6 7}} + assert {[r LPOS mylist c COUNT 2 FIRST -1] == {7 6}} + } + + test {LPOS non existing key} { + assert {[r LPOS mylistxxx c COUNT 0 FIRST 2] eq {}} + } + + test {LPOS no match} { + assert {[r LPOS mylist x COUNT 2 FIRST -1] eq {}} + assert {[r LPOS mylist x FIRST -1] eq {}} + } + + test {LPOS MAXLEN} { + assert {[r LPOS mylist a COUNT 0 MAXLEN 1] == {0}} + assert {[r LPOS mylist c COUNT 0 MAXLEN 1] == {}} + assert {[r LPOS mylist c COUNT 0 MAXLEN 3] == {2}} + assert {[r LPOS mylist c COUNT 0 MAXLEN 3 FIRST -1] == {7 6}} + assert {[r LPOS mylist c COUNT 0 MAXLEN 7 FIRST 2] == {6}} + } + test {LPUSH, RPUSH, LLENGTH, LINDEX, LPOP - ziplist} { # first lpush then rpush assert_equal 1 [r lpush myziplist1 aa]