summaryrefslogtreecommitdiff
path: root/utilities/redis/redis_lists_test.cc
blob: 9005c94cbb3c0ddb08be25f234f94168a0b0b193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/**
 * A test harness for the Redis API built on rocksdb.
 *
 * USAGE: Build with: "make redis_test" (in rocksdb directory).
 *        Run unit tests with: "./redis_test"
 *        Manual/Interactive user testing: "./redis_test -m"
 *        Manual user testing + restart database: "./redis_test -m -d"
 *
 * TODO:  Add LARGE random test cases to verify efficiency and scalability
 *
 * @author Deon Nicholas (dnicholas@fb.com)
 * Copyright 2013 Facebook
 */


#include <iostream>
#include <cctype>

#include "redis_lists.h"
#include "util/testharness.h"
#include "util/random.h"

using namespace rocksdb;
using namespace std;

namespace rocksdb {

class RedisListsTest {
 public:
  static const string kDefaultDbName;
  static Options options;

  RedisListsTest() {
    options.create_if_missing = true;
  }
};

const string RedisListsTest::kDefaultDbName = "/tmp/redisdefaultdb/";
Options RedisListsTest::options = Options();

// operator== and operator<< are defined below for vectors (lists)
// Needed for ASSERT_EQ

// Compare two lists for equality.
bool operator==(const std::vector<std::string>& a,
                const std::vector<std::string>& b) {
  if (a.size() != b.size()) {
    return false;
  }

  int n = a.size();
  for (int i=0; i<n; ++i) {
    if (a[i] != b[i]) {
      return false;
    }
  }

  return true;
}

// Print out a list
ostream& operator<<(ostream& out, const std::vector<std::string>& vec) {
  out << "[";
  int n = vec.size();
  for(int i=0; i<n; ++i) {
    if (i > 0) {
      out << ", ";
    }
    out << vec[i];
  }
  out << "]";
  return out;
}

/// THE TEST CASES BEGIN HERE

// PushRight, Length, Index, Range
TEST(RedisListsTest, SimpleTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Simple PushRight (should return the new length each time)
  ASSERT_EQ(redis.PushRight("k1", "v1"), 1);
  ASSERT_EQ(redis.PushRight("k1", "v2"), 2);
  ASSERT_EQ(redis.PushRight("k1", "v3"), 3);

  // Check Length and Index() functions
  ASSERT_EQ(redis.Length("k1"), 3);        // Check length
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "v1");   // Check valid indices
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "v2");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "v3");

  // Check range function and vectors
  std::vector<std::string> result = redis.Range("k1", 0, 2);   // Get the list
  std::vector<std::string> expected_result(3);
  expected_result[0] = "v1";
  expected_result[1] = "v2";
  expected_result[2] = "v3";
  ASSERT_EQ(result, expected_result);  // Uses my overloaded operator==() above
}

// PushLeft, Length, Index, Range
TEST(RedisListsTest, SimpleTest2) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Simple PushRight
  ASSERT_EQ(redis.PushLeft("k1", "v3"), 1);
  ASSERT_EQ(redis.PushLeft("k1", "v2"), 2);
  ASSERT_EQ(redis.PushLeft("k1", "v1"), 3);

  // Check Length and Index() functions
  ASSERT_EQ(redis.Length("k1"), 3);        // Check length
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "v1");   // Check valid indices
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "v2");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "v3");

  // Check range function and vectors
  std::vector<std::string> result = redis.Range("k1", 0, 2);   // Get the list
  std::vector<std::string> expected_result(3);
  expected_result[0] = "v1";
  expected_result[1] = "v2";
  expected_result[2] = "v3";
  ASSERT_EQ(result, expected_result);  // Uses my overloaded operator==() above
}

// Exhaustive test of the Index() function
TEST(RedisListsTest, IndexTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Empty Index check (return empty and should not crash or edit tempv)
  tempv = "yo";
  ASSERT_TRUE(!redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "yo");
  ASSERT_TRUE(!redis.Index("fda", 3, &tempv));
  ASSERT_EQ(tempv, "yo");
  ASSERT_TRUE(!redis.Index("random", -12391, &tempv));
  ASSERT_EQ(tempv, "yo");

  // Simple Pushes (will yield: [v6, v4, v4, v1, v2, v3]
  redis.PushRight("k1", "v1");
  redis.PushRight("k1", "v2");
  redis.PushRight("k1", "v3");
  redis.PushLeft("k1", "v4");
  redis.PushLeft("k1", "v4");
  redis.PushLeft("k1", "v6");

  // Simple, non-negative indices
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "v6");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "v4");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "v4");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "v1");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "v2");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "v3");

  // Negative indices
  ASSERT_TRUE(redis.Index("k1", -6, &tempv));
  ASSERT_EQ(tempv, "v6");
  ASSERT_TRUE(redis.Index("k1", -5, &tempv));
  ASSERT_EQ(tempv, "v4");
  ASSERT_TRUE(redis.Index("k1", -4, &tempv));
  ASSERT_EQ(tempv, "v4");
  ASSERT_TRUE(redis.Index("k1", -3, &tempv));
  ASSERT_EQ(tempv, "v1");
  ASSERT_TRUE(redis.Index("k1", -2, &tempv));
  ASSERT_EQ(tempv, "v2");
  ASSERT_TRUE(redis.Index("k1", -1, &tempv));
  ASSERT_EQ(tempv, "v3");

  // Out of bounds (return empty, no crash)
  ASSERT_TRUE(!redis.Index("k1", 6, &tempv));
  ASSERT_TRUE(!redis.Index("k1", 123219, &tempv));
  ASSERT_TRUE(!redis.Index("k1", -7, &tempv));
  ASSERT_TRUE(!redis.Index("k1", -129, &tempv));
}


// Exhaustive test of the Range() function
TEST(RedisListsTest, RangeTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Simple Pushes (will yield: [v6, v4, v4, v1, v2, v3])
  redis.PushRight("k1", "v1");
  redis.PushRight("k1", "v2");
  redis.PushRight("k1", "v3");
  redis.PushLeft("k1", "v4");
  redis.PushLeft("k1", "v4");
  redis.PushLeft("k1", "v6");

  // Sanity check (check the length;  make sure it's 6)
  ASSERT_EQ(redis.Length("k1"), 6);

  // Simple range
  std::vector<std::string> res = redis.Range("k1", 1, 4);
  ASSERT_EQ((int)res.size(), 4);
  ASSERT_EQ(res[0], "v4");
  ASSERT_EQ(res[1], "v4");
  ASSERT_EQ(res[2], "v1");
  ASSERT_EQ(res[3], "v2");

  // Negative indices (i.e.: measured from the end)
  res = redis.Range("k1", 2, -1);
  ASSERT_EQ((int)res.size(), 4);
  ASSERT_EQ(res[0], "v4");
  ASSERT_EQ(res[1], "v1");
  ASSERT_EQ(res[2], "v2");
  ASSERT_EQ(res[3], "v3");

  res = redis.Range("k1", -6, -4);
  ASSERT_EQ((int)res.size(), 3);
  ASSERT_EQ(res[0], "v6");
  ASSERT_EQ(res[1], "v4");
  ASSERT_EQ(res[2], "v4");

  res = redis.Range("k1", -1, 5);
  ASSERT_EQ((int)res.size(), 1);
  ASSERT_EQ(res[0], "v3");

  // Partial / Broken indices
  res = redis.Range("k1", -3, 1000000);
  ASSERT_EQ((int)res.size(), 3);
  ASSERT_EQ(res[0], "v1");
  ASSERT_EQ(res[1], "v2");
  ASSERT_EQ(res[2], "v3");

  res = redis.Range("k1", -1000000, 1);
  ASSERT_EQ((int)res.size(), 2);
  ASSERT_EQ(res[0], "v6");
  ASSERT_EQ(res[1], "v4");

  // Invalid indices
  res = redis.Range("k1", 7, 9);
  ASSERT_EQ((int)res.size(), 0);

  res = redis.Range("k1", -8, -7);
  ASSERT_EQ((int)res.size(), 0);

  res = redis.Range("k1", 3, 2);
  ASSERT_EQ((int)res.size(), 0);

  res = redis.Range("k1", 5, -2);
  ASSERT_EQ((int)res.size(), 0);

  // Range matches Index
  res = redis.Range("k1", -6, -4);
  ASSERT_TRUE(redis.Index("k1", -6, &tempv));
  ASSERT_EQ(tempv, res[0]);
  ASSERT_TRUE(redis.Index("k1", -5, &tempv));
  ASSERT_EQ(tempv, res[1]);
  ASSERT_TRUE(redis.Index("k1", -4, &tempv));
  ASSERT_EQ(tempv, res[2]);

  // Last check
  res = redis.Range("k1", 0, -6);
  ASSERT_EQ((int)res.size(), 1);
  ASSERT_EQ(res[0], "v6");
}

// Exhaustive test for InsertBefore(), and InsertAfter()
TEST(RedisListsTest, InsertTest) {
  RedisLists redis(kDefaultDbName, options, true);

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Insert on empty list (return 0, and do not crash)
  ASSERT_EQ(redis.InsertBefore("k1", "non-exist", "a"), 0);
  ASSERT_EQ(redis.InsertAfter("k1", "other-non-exist", "c"), 0);
  ASSERT_EQ(redis.Length("k1"), 0);

  // Push some preliminary stuff [g, f, e, d, c, b, a]
  redis.PushLeft("k1", "a");
  redis.PushLeft("k1", "b");
  redis.PushLeft("k1", "c");
  redis.PushLeft("k1", "d");
  redis.PushLeft("k1", "e");
  redis.PushLeft("k1", "f");
  redis.PushLeft("k1", "g");
  ASSERT_EQ(redis.Length("k1"), 7);

  // Test InsertBefore
  int newLength = redis.InsertBefore("k1", "e", "hello");
  ASSERT_EQ(newLength, 8);
  ASSERT_EQ(redis.Length("k1"), newLength);
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "f");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "e");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "hello");

  // Test InsertAfter
  newLength =  redis.InsertAfter("k1", "c", "bye");
  ASSERT_EQ(newLength, 9);
  ASSERT_EQ(redis.Length("k1"), newLength);
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "bye");

  // Test bad value on InsertBefore
  newLength = redis.InsertBefore("k1", "yo", "x");
  ASSERT_EQ(newLength, 9);
  ASSERT_EQ(redis.Length("k1"), newLength);

  // Test bad value on InsertAfter
  newLength = redis.InsertAfter("k1", "xxxx", "y");
  ASSERT_EQ(newLength, 9);
  ASSERT_EQ(redis.Length("k1"), newLength);

  // Test InsertBefore beginning
  newLength = redis.InsertBefore("k1", "g", "begggggggggggggggg");
  ASSERT_EQ(newLength, 10);
  ASSERT_EQ(redis.Length("k1"), newLength);

  // Test InsertAfter end
  newLength = redis.InsertAfter("k1", "a", "enddd");
  ASSERT_EQ(newLength, 11);
  ASSERT_EQ(redis.Length("k1"), newLength);

  // Make sure nothing weird happened.
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "begggggggggggggggg");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "g");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "f");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "hello");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "e");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "d");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "c");
  ASSERT_TRUE(redis.Index("k1", 7, &tempv));
  ASSERT_EQ(tempv, "bye");
  ASSERT_TRUE(redis.Index("k1", 8, &tempv));
  ASSERT_EQ(tempv, "b");
  ASSERT_TRUE(redis.Index("k1", 9, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_TRUE(redis.Index("k1", 10, &tempv));
  ASSERT_EQ(tempv, "enddd");
}

// Exhaustive test of Set function
TEST(RedisListsTest, SetTest) {
  RedisLists redis(kDefaultDbName, options, true);

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Set on empty list (return false, and do not crash)
  ASSERT_EQ(redis.Set("k1", 7, "a"), false);
  ASSERT_EQ(redis.Set("k1", 0, "a"), false);
  ASSERT_EQ(redis.Set("k1", -49, "cx"), false);
  ASSERT_EQ(redis.Length("k1"), 0);

  // Push some preliminary stuff [g, f, e, d, c, b, a]
  redis.PushLeft("k1", "a");
  redis.PushLeft("k1", "b");
  redis.PushLeft("k1", "c");
  redis.PushLeft("k1", "d");
  redis.PushLeft("k1", "e");
  redis.PushLeft("k1", "f");
  redis.PushLeft("k1", "g");
  ASSERT_EQ(redis.Length("k1"), 7);

  // Test Regular Set
  ASSERT_TRUE(redis.Set("k1", 0, "0"));
  ASSERT_TRUE(redis.Set("k1", 3, "3"));
  ASSERT_TRUE(redis.Set("k1", 6, "6"));
  ASSERT_TRUE(redis.Set("k1", 2, "2"));
  ASSERT_TRUE(redis.Set("k1", 5, "5"));
  ASSERT_TRUE(redis.Set("k1", 1, "1"));
  ASSERT_TRUE(redis.Set("k1", 4, "4"));

  ASSERT_EQ(redis.Length("k1"), 7); // Size should not change
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "0");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "1");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "2");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "3");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "4");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "5");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "6");

  // Set with negative indices
  ASSERT_TRUE(redis.Set("k1", -7, "a"));
  ASSERT_TRUE(redis.Set("k1", -4, "d"));
  ASSERT_TRUE(redis.Set("k1", -1, "g"));
  ASSERT_TRUE(redis.Set("k1", -5, "c"));
  ASSERT_TRUE(redis.Set("k1", -2, "f"));
  ASSERT_TRUE(redis.Set("k1", -6, "b"));
  ASSERT_TRUE(redis.Set("k1", -3, "e"));

  ASSERT_EQ(redis.Length("k1"), 7); // Size should not change
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "b");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "c");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "d");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "e");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "f");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "g");

  // Bad indices (just out-of-bounds / off-by-one check)
  ASSERT_EQ(redis.Set("k1", -8, "off-by-one in negative index"), false);
  ASSERT_EQ(redis.Set("k1", 7, "off-by-one-error in positive index"), false);
  ASSERT_EQ(redis.Set("k1", 43892, "big random index should fail"), false);
  ASSERT_EQ(redis.Set("k1", -21391, "large negative index should fail"), false);

  // One last check (to make sure nothing weird happened)
  ASSERT_EQ(redis.Length("k1"), 7); // Size should not change
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "b");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "c");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "d");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "e");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "f");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "g");
}

// Testing Insert, Push, and Set, in a mixed environment
TEST(RedisListsTest, InsertPushSetTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // A series of pushes and insertions
  // Will result in [newbegin, z, a, aftera, x, newend]
  // Also, check the return value sometimes (should return length)
  int lengthCheck;
  lengthCheck = redis.PushLeft("k1", "a");
  ASSERT_EQ(lengthCheck, 1);
  redis.PushLeft("k1", "z");
  redis.PushRight("k1", "x");
  lengthCheck = redis.InsertAfter("k1", "a", "aftera");
  ASSERT_EQ(lengthCheck , 4);
  redis.InsertBefore("k1", "z", "newbegin");  // InsertBefore beginning of list
  redis.InsertAfter("k1", "x", "newend");     // InsertAfter end of list

  // Check
  std::vector<std::string> res = redis.Range("k1", 0, -1); // Get the list
  ASSERT_EQ((int)res.size(), 6);
  ASSERT_EQ(res[0], "newbegin");
  ASSERT_EQ(res[5], "newend");
  ASSERT_EQ(res[3], "aftera");

  // Testing duplicate values/pivots (multiple occurrences of 'a')
  ASSERT_TRUE(redis.Set("k1", 0, "a"));     // [a, z, a, aftera, x, newend]
  redis.InsertAfter("k1", "a", "happy");    // [a, happy, z, a, aftera, ...]
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "happy");
  redis.InsertBefore("k1", "a", "sad");     // [sad, a, happy, z, a, aftera, ...]
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "sad");
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "happy");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "aftera");
  redis.InsertAfter("k1", "a", "zz");         // [sad, a, zz, happy, z, a, aftera, ...]
  ASSERT_TRUE(redis.Index("k1", 2, &tempv));
  ASSERT_EQ(tempv, "zz");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "aftera");
  ASSERT_TRUE(redis.Set("k1", 1, "nota"));    // [sad, nota, zz, happy, z, a, ...]
  redis.InsertBefore("k1", "a", "ba");        // [sad, nota, zz, happy, z, ba, a, ...]
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "z");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "ba");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "a");

  // We currently have: [sad, nota, zz, happy, z, ba, a, aftera, x, newend]
  // redis.Print("k1");   // manually check

  // Test Inserting before/after non-existent values
  lengthCheck = redis.Length("k1"); // Ensure that the length doesnt change
  ASSERT_EQ(lengthCheck, 10);
  ASSERT_EQ(redis.InsertBefore("k1", "non-exist", "randval"), lengthCheck);
  ASSERT_EQ(redis.InsertAfter("k1", "nothing", "a"), lengthCheck);
  ASSERT_EQ(redis.InsertAfter("randKey", "randVal", "ranValue"), 0); // Empty
  ASSERT_EQ(redis.Length("k1"), lengthCheck); // The length should not change

  // Simply Test the Set() function
  redis.Set("k1", 5, "ba2");
  redis.InsertBefore("k1", "ba2", "beforeba2");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "z");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "beforeba2");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "ba2");
  ASSERT_TRUE(redis.Index("k1", 7, &tempv));
  ASSERT_EQ(tempv, "a");

  // We have: [sad, nota, zz, happy, z, beforeba2, ba2, a, aftera, x, newend]

  // Set() with negative indices
  redis.Set("k1", -1, "endprank");
  ASSERT_TRUE(!redis.Index("k1", 11, &tempv));
  ASSERT_TRUE(redis.Index("k1", 10, &tempv));
  ASSERT_EQ(tempv, "endprank"); // Ensure Set worked correctly
  redis.Set("k1", -11, "t");
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "t");

  // Test out of bounds Set
  ASSERT_EQ(redis.Set("k1", -12, "ssd"), false);
  ASSERT_EQ(redis.Set("k1", 11, "sasd"), false);
  ASSERT_EQ(redis.Set("k1", 1200, "big"), false);
}

// Testing Trim, Pop
TEST(RedisListsTest, TrimPopTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // A series of pushes and insertions
  // Will result in [newbegin, z, a, aftera, x, newend]
  redis.PushLeft("k1", "a");
  redis.PushLeft("k1", "z");
  redis.PushRight("k1", "x");
  redis.InsertBefore("k1", "z", "newbegin");    // InsertBefore start of list
  redis.InsertAfter("k1", "x", "newend");       // InsertAfter end of list
  redis.InsertAfter("k1", "a", "aftera");

  // Simple PopLeft/Right test
  ASSERT_TRUE(redis.PopLeft("k1", &tempv));
  ASSERT_EQ(tempv, "newbegin");
  ASSERT_EQ(redis.Length("k1"), 5);
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "z");
  ASSERT_TRUE(redis.PopRight("k1", &tempv));
  ASSERT_EQ(tempv, "newend");
  ASSERT_EQ(redis.Length("k1"), 4);
  ASSERT_TRUE(redis.Index("k1", -1, &tempv));
  ASSERT_EQ(tempv, "x");

  // Now have: [z, a, aftera, x]

  // Test Trim
  ASSERT_TRUE(redis.Trim("k1", 0, -1));       // [z, a, aftera, x] (do nothing)
  ASSERT_EQ(redis.Length("k1"), 4);
  ASSERT_TRUE(redis.Trim("k1", 0, 2));                     // [z, a, aftera]
  ASSERT_EQ(redis.Length("k1"), 3);
  ASSERT_TRUE(redis.Index("k1", -1, &tempv));
  ASSERT_EQ(tempv, "aftera");
  ASSERT_TRUE(redis.Trim("k1", 1, 1));                     // [a]
  ASSERT_EQ(redis.Length("k1"), 1);
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "a");

  // Test out of bounds (empty) trim
  ASSERT_TRUE(redis.Trim("k1", 1, 0));
  ASSERT_EQ(redis.Length("k1"), 0);

  // Popping with empty list (return empty without error)
  ASSERT_TRUE(!redis.PopLeft("k1", &tempv));
  ASSERT_TRUE(!redis.PopRight("k1", &tempv));
  ASSERT_TRUE(redis.Trim("k1", 0, 5));

  // Exhaustive Trim test (negative and invalid indices)
  // Will start in [newbegin, z, a, aftera, x, newend]
  redis.PushLeft("k1", "a");
  redis.PushLeft("k1", "z");
  redis.PushRight("k1", "x");
  redis.InsertBefore("k1", "z", "newbegin");    // InsertBefore start of list
  redis.InsertAfter("k1", "x", "newend");       // InsertAfter end of list
  redis.InsertAfter("k1", "a", "aftera");
  ASSERT_TRUE(redis.Trim("k1", -6, -1));                     // Should do nothing
  ASSERT_EQ(redis.Length("k1"), 6);
  ASSERT_TRUE(redis.Trim("k1", 1, -2));
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "z");
  ASSERT_TRUE(redis.Index("k1", 3, &tempv));
  ASSERT_EQ(tempv, "x");
  ASSERT_EQ(redis.Length("k1"), 4);
  ASSERT_TRUE(redis.Trim("k1", -3, -2));
  ASSERT_EQ(redis.Length("k1"), 2);
}

// Testing Remove, RemoveFirst, RemoveLast
TEST(RedisListsTest, RemoveTest) {
  RedisLists redis(kDefaultDbName, options, true);   // Destructive

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // A series of pushes and insertions
  // Will result in [newbegin, z, a, aftera, x, newend, a, a]
  redis.PushLeft("k1", "a");
  redis.PushLeft("k1", "z");
  redis.PushRight("k1", "x");
  redis.InsertBefore("k1", "z", "newbegin");    // InsertBefore start of list
  redis.InsertAfter("k1", "x", "newend");       // InsertAfter end of list
  redis.InsertAfter("k1", "a", "aftera");
  redis.PushRight("k1", "a");
  redis.PushRight("k1", "a");

  // Verify
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "newbegin");
  ASSERT_TRUE(redis.Index("k1", -1, &tempv));
  ASSERT_EQ(tempv, "a");

  // Check RemoveFirst (Remove the first two 'a')
  // Results in [newbegin, z, aftera, x, newend, a]
  int numRemoved = redis.Remove("k1", 2, "a");
  ASSERT_EQ(numRemoved, 2);
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "newbegin");
  ASSERT_TRUE(redis.Index("k1", 1, &tempv));
  ASSERT_EQ(tempv, "z");
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "newend");
  ASSERT_TRUE(redis.Index("k1", 5, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_EQ(redis.Length("k1"), 6);

  // Repopulate some stuff
  // Results in: [x, x, x, x, x, newbegin, z, x, aftera, x, newend, a, x]
  redis.PushLeft("k1", "x");
  redis.PushLeft("k1", "x");
  redis.PushLeft("k1", "x");
  redis.PushLeft("k1", "x");
  redis.PushLeft("k1", "x");
  redis.PushRight("k1", "x");
  redis.InsertAfter("k1", "z", "x");

  // Test removal from end
  numRemoved = redis.Remove("k1", -2, "x");
  ASSERT_EQ(numRemoved, 2);
  ASSERT_TRUE(redis.Index("k1", 8, &tempv));
  ASSERT_EQ(tempv, "aftera");
  ASSERT_TRUE(redis.Index("k1", 9, &tempv));
  ASSERT_EQ(tempv, "newend");
  ASSERT_TRUE(redis.Index("k1", 10, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_TRUE(!redis.Index("k1", 11, &tempv));
  numRemoved = redis.Remove("k1", -2, "x");
  ASSERT_EQ(numRemoved, 2);
  ASSERT_TRUE(redis.Index("k1", 4, &tempv));
  ASSERT_EQ(tempv, "newbegin");
  ASSERT_TRUE(redis.Index("k1", 6, &tempv));
  ASSERT_EQ(tempv, "aftera");

  // We now have: [x, x, x, x, newbegin, z, aftera, newend, a]
  ASSERT_EQ(redis.Length("k1"), 9);
  ASSERT_TRUE(redis.Index("k1", -1, &tempv));
  ASSERT_EQ(tempv, "a");
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "x");

  // Test over-shooting (removing more than there exists)
  numRemoved = redis.Remove("k1", -9000, "x");
  ASSERT_EQ(numRemoved , 4);    // Only really removed 4
  ASSERT_EQ(redis.Length("k1"), 5);
  ASSERT_TRUE(redis.Index("k1", 0, &tempv));
  ASSERT_EQ(tempv, "newbegin");
  numRemoved = redis.Remove("k1", 1, "x");
  ASSERT_EQ(numRemoved, 0);

  // Try removing ALL!
  numRemoved = redis.Remove("k1", 0, "newbegin");   // REMOVE 0 will remove all!
  ASSERT_EQ(numRemoved, 1);

  // Removal from an empty-list
  ASSERT_TRUE(redis.Trim("k1", 1, 0));
  numRemoved = redis.Remove("k1", 1, "z");
  ASSERT_EQ(numRemoved, 0);
}


// Test Multiple keys and Persistence
TEST(RedisListsTest, PersistenceMultiKeyTest) {

  string tempv; // Used below for all Index(), PopRight(), PopLeft()

  // Block one: populate a single key in the database
  {
    RedisLists redis(kDefaultDbName, options, true);   // Destructive

    // A series of pushes and insertions
    // Will result in [newbegin, z, a, aftera, x, newend, a, a]
    redis.PushLeft("k1", "a");
    redis.PushLeft("k1", "z");
    redis.PushRight("k1", "x");
    redis.InsertBefore("k1", "z", "newbegin");    // InsertBefore start of list
    redis.InsertAfter("k1", "x", "newend");       // InsertAfter end of list
    redis.InsertAfter("k1", "a", "aftera");
    redis.PushRight("k1", "a");
    redis.PushRight("k1", "a");

    ASSERT_TRUE(redis.Index("k1", 3, &tempv));
    ASSERT_EQ(tempv, "aftera");
  }

  // Block two: make sure changes were saved and add some other key
  {
    RedisLists redis(kDefaultDbName, options, false); // Persistent, non-destructive

    // Check
    ASSERT_EQ(redis.Length("k1"), 8);
    ASSERT_TRUE(redis.Index("k1", 3, &tempv));
    ASSERT_EQ(tempv, "aftera");

    redis.PushRight("k2", "randomkey");
    redis.PushLeft("k2", "sas");

    redis.PopLeft("k1", &tempv);
  }

  // Block three: Verify the changes from block 2
  {
    RedisLists redis(kDefaultDbName, options, false); // Presistent, non-destructive

    // Check
    ASSERT_EQ(redis.Length("k1"), 7);
    ASSERT_EQ(redis.Length("k2"), 2);
    ASSERT_TRUE(redis.Index("k1", 0, &tempv));
    ASSERT_EQ(tempv, "z");
    ASSERT_TRUE(redis.Index("k2", -2, &tempv));
    ASSERT_EQ(tempv, "sas");
  }
}

/// THE manual REDIS TEST begins here
/// THIS WILL ONLY OCCUR IF YOU RUN: ./redis_test -m

void MakeUpper(std::string* const s) {
  int len = s->length();
  for(int i=0; i<len; ++i) {
    (*s)[i] = toupper((*s)[i]); // C-version defined in <ctype.h>
  }
}

/// Allows the user to enter in REDIS commands into the command-line.
/// This is useful for manual / interacticve testing / debugging.
///  Use destructive=true to clean the database before use.
///  Use destructive=false to remember the previous state (i.e.: persistent)
/// Should be called from main function.
int manual_redis_test(bool destructive){
  RedisLists redis(RedisListsTest::kDefaultDbName,
                   RedisListsTest::options,
                   destructive);

  // TODO: Right now, please use spaces to separate each word.
  //  In actual redis, you can use quotes to specify compound values
  //  Example: RPUSH mylist "this is a compound value"

  std::string command;
  while(true) {
    cin >> command;
    MakeUpper(&command);

    if (command == "LINSERT") {
      std::string k, t, p, v;
      cin >> k >> t >> p >> v;
      MakeUpper(&t);
      if (t=="BEFORE") {
        std::cout << redis.InsertBefore(k, p, v) << std::endl;
      } else if (t=="AFTER") {
        std::cout << redis.InsertAfter(k, p, v) << std::endl;
      }
    } else if (command == "LPUSH") {
      std::string k, v;
      std::cin >> k >> v;
      redis.PushLeft(k, v);
    } else if (command == "RPUSH") {
      std::string k, v;
      std::cin >> k >> v;
      redis.PushRight(k, v);
    } else if (command == "LPOP") {
      std::string k;
      std::cin >> k;
      string res;
      redis.PopLeft(k, &res);
      std::cout << res << std::endl;
    } else if (command == "RPOP") {
      std::string k;
      std::cin >> k;
      string res;
      redis.PopRight(k, &res);
      std::cout << res << std::endl;
    } else if (command == "LREM") {
      std::string k;
      int amt;
      std::string v;

      std::cin >> k >> amt >> v;
      std::cout << redis.Remove(k, amt, v) << std::endl;
    } else if (command == "LLEN") {
      std::string k;
      std::cin >> k;
      std::cout << redis.Length(k) << std::endl;
    } else if (command == "LRANGE") {
      std::string k;
      int i, j;
      std::cin >> k >> i >> j;
      std::vector<std::string> res = redis.Range(k, i, j);
      for (auto it = res.begin(); it != res.end(); ++it) {
        std::cout << " " << (*it);
      }
      std::cout << std::endl;
    } else if (command == "LTRIM") {
      std::string k;
      int i, j;
      std::cin >> k >> i >> j;
      redis.Trim(k, i, j);
    } else if (command == "LSET") {
      std::string k;
      int idx;
      std::string v;
      cin >> k >> idx >> v;
      redis.Set(k, idx, v);
    } else if (command == "LINDEX") {
      std::string k;
      int idx;
      std::cin >> k >> idx;
      string res;
      redis.Index(k, idx, &res);
      std::cout << res << std::endl;
    } else if (command == "PRINT") {      // Added by Deon
      std::string k;
      cin >> k;
      redis.Print(k);
    } else if (command == "QUIT") {
      return 0;
    } else {
      std::cout << "unknown command: " << command << std::endl;
    }
  }
}

} // namespace rocksdb


// USAGE: "./redis_test" for default (unit tests)
//        "./redis_test -m" for manual testing (redis command api)
//        "./redis_test -m -d" for destructive manual test (erase db before use)


// Check for "want" argument in the argument list
bool found_arg(int argc, char* argv[], const char* want){
  for(int i=1; i<argc; ++i){
    if (strcmp(argv[i], want) == 0) {
      return true;
    }
  }
  return false;
}

// Will run unit tests.
// However, if -m is specified, it will do user manual/interactive testing
// -m -d is manual and destructive (will clear the database before use)
int main(int argc, char* argv[]) {
  if (found_arg(argc, argv, "-m")) {
    bool destructive = found_arg(argc, argv, "-d");
    return rocksdb::manual_redis_test(destructive);
  } else {
    return rocksdb::test::RunAllTests();
  }
}