github.com/Finschia/finschia-sdk@v0.49.1/store/cachekv/search_test.go (about)

     1  package cachekv
     2  
     3  import "testing"
     4  
     5  func TestFindStartIndex(t *testing.T) {
     6  	tests := []struct {
     7  		name    string
     8  		sortedL []string
     9  		query   string
    10  		want    int
    11  	}{
    12  		{
    13  			name:    "non-existent value",
    14  			sortedL: []string{"a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    15  			query:   "o",
    16  			want:    8,
    17  		},
    18  		{
    19  			name:    "dupes start at index 0",
    20  			sortedL: []string{"a", "a", "a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    21  			query:   "a",
    22  			want:    0,
    23  		},
    24  		{
    25  			name:    "dupes start at non-index 0",
    26  			sortedL: []string{"a", "c", "c", "c", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    27  			query:   "c",
    28  			want:    1,
    29  		},
    30  		{
    31  			name:    "at end",
    32  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z"},
    33  			query:   "z",
    34  			want:    7,
    35  		},
    36  		{
    37  			name:    "dupes at end",
    38  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z", "z", "z", "z"},
    39  			query:   "z",
    40  			want:    7,
    41  		},
    42  		{
    43  			name:    "entirely dupes",
    44  			sortedL: []string{"z", "z", "z", "z", "z"},
    45  			query:   "z",
    46  			want:    0,
    47  		},
    48  		{
    49  			name:    "non-existent but within >=start",
    50  			sortedL: []string{"z", "z", "z", "z", "z"},
    51  			query:   "p",
    52  			want:    0,
    53  		},
    54  		{
    55  			name:    "non-existent and out of range",
    56  			sortedL: []string{"d", "e", "f", "g", "h"},
    57  			query:   "z",
    58  			want:    -1,
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			body := tt.sortedL
    65  			got := findStartIndex(body, tt.query)
    66  			if got != tt.want {
    67  				t.Fatalf("Got: %d, want: %d", got, tt.want)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestFindEndIndex(t *testing.T) {
    74  	tests := []struct {
    75  		name    string
    76  		sortedL []string
    77  		query   string
    78  		want    int
    79  	}{
    80  		{
    81  			name:    "non-existent value",
    82  			sortedL: []string{"a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    83  			query:   "o",
    84  			want:    7,
    85  		},
    86  		{
    87  			name:    "dupes start at index 0",
    88  			sortedL: []string{"a", "a", "a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    89  			query:   "a",
    90  			want:    0,
    91  		},
    92  		{
    93  			name:    "dupes start at non-index 0",
    94  			sortedL: []string{"a", "c", "c", "c", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    95  			query:   "c",
    96  			want:    1,
    97  		},
    98  		{
    99  			name:    "at end",
   100  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z"},
   101  			query:   "z",
   102  			want:    7,
   103  		},
   104  		{
   105  			name:    "dupes at end",
   106  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z", "z", "z", "z"},
   107  			query:   "z",
   108  			want:    7,
   109  		},
   110  		{
   111  			name:    "entirely dupes",
   112  			sortedL: []string{"z", "z", "z", "z", "z"},
   113  			query:   "z",
   114  			want:    0,
   115  		},
   116  		{
   117  			name:    "non-existent and out of range",
   118  			sortedL: []string{"z", "z", "z", "z", "z"},
   119  			query:   "p",
   120  			want:    -1,
   121  		},
   122  		{
   123  			name:    "non-existent and out of range",
   124  			sortedL: []string{"d", "e", "f", "g", "h"},
   125  			query:   "z",
   126  			want:    4,
   127  		},
   128  	}
   129  
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			body := tt.sortedL
   133  			got := findEndIndex(body, tt.query)
   134  			if got != tt.want {
   135  				t.Fatalf("Got: %d, want: %d", got, tt.want)
   136  			}
   137  		})
   138  	}
   139  }