kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/pager/pager_test.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package pager
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"kythe.io/kythe/go/test/testutil"
    24  )
    25  
    26  type testSet struct {
    27  	Head   string
    28  	Total  int
    29  	Groups []*testGroup
    30  	Pages  int
    31  }
    32  
    33  type testPage struct {
    34  	Index int
    35  	Key   string
    36  	Group *testGroup
    37  }
    38  
    39  type testGroup struct {
    40  	Key  string
    41  	Vals []int
    42  }
    43  
    44  func TestPager(t *testing.T) {
    45  	var outputSets, expectedSets []*testSet
    46  	var outputPages, expectedPages []*testPage
    47  
    48  	p := &SetPager{
    49  		MaxPageSize: 4,
    50  
    51  		OutputSet: func(_ context.Context, total int, s Set, grps []Group) error {
    52  			ts := s.(*testSet)
    53  			ts.Total = total
    54  			for _, g := range grps {
    55  				ts.Groups = append(ts.Groups, g.(*testGroup))
    56  			}
    57  			outputSets = append(outputSets, ts)
    58  			return nil
    59  		},
    60  		OutputPage: func(_ context.Context, s Set, g Group) error {
    61  			ts := s.(*testSet)
    62  			outputPages = append(outputPages, &testPage{
    63  				Index: ts.Pages,
    64  				Group: g.(*testGroup),
    65  			})
    66  			ts.Pages++
    67  			return nil
    68  		},
    69  
    70  		NewSet: func(h Head) Set {
    71  			return &testSet{Head: h.(string)}
    72  		},
    73  		Combine: func(l, r Group) Group {
    74  			lg, rg := l.(*testGroup), r.(*testGroup)
    75  			if lg.Key != rg.Key {
    76  				return nil
    77  			}
    78  			lg.Vals = append(lg.Vals, rg.Vals...)
    79  			return lg
    80  		},
    81  		Split: func(total int, g Group) (Group, Group) {
    82  			tg := g.(*testGroup)
    83  			ng := &testGroup{
    84  				Key:  tg.Key,
    85  				Vals: tg.Vals[:total],
    86  			}
    87  			tg.Vals = tg.Vals[total:]
    88  			return ng, tg
    89  		},
    90  		Size: func(g Group) int { return len(g.(*testGroup).Vals) },
    91  	}
    92  
    93  	ctx := context.Background()
    94  	testutil.Fatalf(t, "StartSet error: %v", p.StartSet(ctx, "head key"))
    95  	testutil.Fatalf(t, "AddGroup error: %v", p.AddGroup(ctx, &testGroup{
    96  		Key:  "key1",
    97  		Vals: []int{11, 12, 13},
    98  	}))
    99  	testutil.Fatalf(t, "AddGroup error: %v", p.AddGroup(ctx, &testGroup{
   100  		Key:  "key1",
   101  		Vals: []int{14, 15, 16},
   102  	}))
   103  	expectedPages = append(expectedPages, &testPage{
   104  		Index: 0,
   105  		Group: &testGroup{
   106  			Key:  "key1",
   107  			Vals: []int{11, 12, 13, 14},
   108  		},
   109  	})
   110  
   111  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   112  		t.Fatalf("error checking Sets: %v", err)
   113  	}
   114  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   115  		t.Fatalf("error checking Pages: %v", err)
   116  	}
   117  
   118  	testutil.Fatalf(t, "AddGroup error: %v", p.AddGroup(ctx, &testGroup{
   119  		Key:  "key2",
   120  		Vals: []int{21},
   121  	}))
   122  	testutil.Fatalf(t, "AddGroup error: %v", p.AddGroup(ctx, &testGroup{
   123  		Key:  "key2",
   124  		Vals: []int{22, 23},
   125  	}))
   126  	expectedPages = append(expectedPages, &testPage{
   127  		Index: 1,
   128  		Group: &testGroup{
   129  			Key:  "key2",
   130  			Vals: []int{21, 22, 23},
   131  		},
   132  	})
   133  
   134  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   135  		t.Fatalf("error checking Sets: %v", err)
   136  	}
   137  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   138  		t.Fatalf("error checking Pages: %v", err)
   139  	}
   140  
   141  	testutil.Fatalf(t, "StartSet error: %v", p.StartSet(ctx, "next set"))
   142  	expectedSets = append(expectedSets, &testSet{
   143  		Head:  "head key",
   144  		Total: 9,
   145  		Groups: []*testGroup{{
   146  			Key:  "key1",
   147  			Vals: []int{15, 16},
   148  		}},
   149  		Pages: len(expectedPages),
   150  	})
   151  
   152  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   153  		t.Fatalf("error checking Sets: %v", err)
   154  	}
   155  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   156  		t.Fatalf("error checking Pages: %v", err)
   157  	}
   158  
   159  	testutil.Fatalf(t, "StartSet error: %v", p.StartSet(ctx, "final set"))
   160  	expectedSets = append(expectedSets, &testSet{
   161  		Head: "next set",
   162  	})
   163  
   164  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   165  		t.Fatalf("error checking Sets: %v", err)
   166  	}
   167  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   168  		t.Fatalf("error checking Pages: %v", err)
   169  	}
   170  
   171  	testutil.Fatalf(t, "AddGroup error: %v", p.AddGroup(ctx, &testGroup{
   172  		Key:  "key0",
   173  		Vals: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
   174  	}))
   175  	expectedPages = append(expectedPages, &testPage{
   176  		Index: 0,
   177  		Group: &testGroup{
   178  			Key:  "key0",
   179  			Vals: []int{1, 2, 3, 4},
   180  		},
   181  	})
   182  	expectedPages = append(expectedPages, &testPage{
   183  		Index: 1,
   184  		Group: &testGroup{
   185  			Key:  "key0",
   186  			Vals: []int{5, 6, 7, 8},
   187  		},
   188  	})
   189  
   190  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   191  		t.Fatalf("error checking Sets: %v", err)
   192  	}
   193  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   194  		t.Fatalf("error checking Pages: %v", err)
   195  	}
   196  
   197  	testutil.Fatalf(t, "Flush error: %v", p.Flush(ctx))
   198  	expectedSets = append(expectedSets, &testSet{
   199  		Head:  "final set",
   200  		Total: 12,
   201  		Groups: []*testGroup{{
   202  			Key:  "key0",
   203  			Vals: []int{9, 10, 11, 12},
   204  		}},
   205  		Pages: 2,
   206  	})
   207  
   208  	if err := testutil.DeepEqual(expectedSets, outputSets); err != nil {
   209  		t.Fatalf("error checking Sets: %v", err)
   210  	}
   211  	if err := testutil.DeepEqual(expectedPages, outputPages); err != nil {
   212  		t.Fatalf("error checking Pages: %v", err)
   213  	}
   214  }