github.com/Finschia/finschia-sdk@v0.48.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  		tt := tt
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			body := tt.sortedL
    66  			got := findStartIndex(body, tt.query)
    67  			if got != tt.want {
    68  				t.Fatalf("Got: %d, want: %d", got, tt.want)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestFindEndIndex(t *testing.T) {
    75  	tests := []struct {
    76  		name    string
    77  		sortedL []string
    78  		query   string
    79  		want    int
    80  	}{
    81  		{
    82  			name:    "non-existent value",
    83  			sortedL: []string{"a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    84  			query:   "o",
    85  			want:    7,
    86  		},
    87  		{
    88  			name:    "dupes start at index 0",
    89  			sortedL: []string{"a", "a", "a", "b", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    90  			query:   "a",
    91  			want:    0,
    92  		},
    93  		{
    94  			name:    "dupes start at non-index 0",
    95  			sortedL: []string{"a", "c", "c", "c", "c", "d", "e", "l", "m", "n", "u", "v", "w", "x", "y", "z"},
    96  			query:   "c",
    97  			want:    1,
    98  		},
    99  		{
   100  			name:    "at end",
   101  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z"},
   102  			query:   "z",
   103  			want:    7,
   104  		},
   105  		{
   106  			name:    "dupes at end",
   107  			sortedL: []string{"a", "e", "u", "v", "w", "x", "y", "z", "z", "z", "z"},
   108  			query:   "z",
   109  			want:    7,
   110  		},
   111  		{
   112  			name:    "entirely dupes",
   113  			sortedL: []string{"z", "z", "z", "z", "z"},
   114  			query:   "z",
   115  			want:    0,
   116  		},
   117  		{
   118  			name:    "non-existent and out of range",
   119  			sortedL: []string{"z", "z", "z", "z", "z"},
   120  			query:   "p",
   121  			want:    -1,
   122  		},
   123  		{
   124  			name:    "non-existent and out of range",
   125  			sortedL: []string{"d", "e", "f", "g", "h"},
   126  			query:   "z",
   127  			want:    4,
   128  		},
   129  	}
   130  
   131  	for _, tt := range tests {
   132  		tt := tt
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			body := tt.sortedL
   135  			got := findEndIndex(body, tt.query)
   136  			if got != tt.want {
   137  				t.Fatalf("Got: %d, want: %d", got, tt.want)
   138  			}
   139  		})
   140  	}
   141  }