github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/network/stream/intervals/intervals_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:48</date>
    10  //</624342675474092032>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package intervals
    29  
    30  import "testing"
    31  
    32  //
    33  //
    34  func Test(t *testing.T) {
    35  	for i, tc := range []struct {
    36  		startLimit uint64
    37  		initial    [][2]uint64
    38  		start      uint64
    39  		end        uint64
    40  		expected   string
    41  		nextStart  uint64
    42  		nextEnd    uint64
    43  		last       uint64
    44  	}{
    45  		{
    46  			initial:   nil,
    47  			start:     0,
    48  			end:       0,
    49  			expected:  "[[0 0]]",
    50  			nextStart: 1,
    51  			nextEnd:   0,
    52  			last:      0,
    53  		},
    54  		{
    55  			initial:   nil,
    56  			start:     0,
    57  			end:       10,
    58  			expected:  "[[0 10]]",
    59  			nextStart: 11,
    60  			nextEnd:   0,
    61  			last:      10,
    62  		},
    63  		{
    64  			initial:   nil,
    65  			start:     5,
    66  			end:       15,
    67  			expected:  "[[5 15]]",
    68  			nextStart: 0,
    69  			nextEnd:   4,
    70  			last:      15,
    71  		},
    72  		{
    73  			initial:   [][2]uint64{{0, 0}},
    74  			start:     0,
    75  			end:       0,
    76  			expected:  "[[0 0]]",
    77  			nextStart: 1,
    78  			nextEnd:   0,
    79  			last:      0,
    80  		},
    81  		{
    82  			initial:   [][2]uint64{{0, 0}},
    83  			start:     5,
    84  			end:       15,
    85  			expected:  "[[0 0] [5 15]]",
    86  			nextStart: 1,
    87  			nextEnd:   4,
    88  			last:      15,
    89  		},
    90  		{
    91  			initial:   [][2]uint64{{5, 15}},
    92  			start:     5,
    93  			end:       15,
    94  			expected:  "[[5 15]]",
    95  			nextStart: 0,
    96  			nextEnd:   4,
    97  			last:      15,
    98  		},
    99  		{
   100  			initial:   [][2]uint64{{5, 15}},
   101  			start:     5,
   102  			end:       20,
   103  			expected:  "[[5 20]]",
   104  			nextStart: 0,
   105  			nextEnd:   4,
   106  			last:      20,
   107  		},
   108  		{
   109  			initial:   [][2]uint64{{5, 15}},
   110  			start:     10,
   111  			end:       20,
   112  			expected:  "[[5 20]]",
   113  			nextStart: 0,
   114  			nextEnd:   4,
   115  			last:      20,
   116  		},
   117  		{
   118  			initial:   [][2]uint64{{5, 15}},
   119  			start:     0,
   120  			end:       20,
   121  			expected:  "[[0 20]]",
   122  			nextStart: 21,
   123  			nextEnd:   0,
   124  			last:      20,
   125  		},
   126  		{
   127  			initial:   [][2]uint64{{5, 15}},
   128  			start:     2,
   129  			end:       10,
   130  			expected:  "[[2 15]]",
   131  			nextStart: 0,
   132  			nextEnd:   1,
   133  			last:      15,
   134  		},
   135  		{
   136  			initial:   [][2]uint64{{5, 15}},
   137  			start:     2,
   138  			end:       4,
   139  			expected:  "[[2 15]]",
   140  			nextStart: 0,
   141  			nextEnd:   1,
   142  			last:      15,
   143  		},
   144  		{
   145  			initial:   [][2]uint64{{5, 15}},
   146  			start:     2,
   147  			end:       5,
   148  			expected:  "[[2 15]]",
   149  			nextStart: 0,
   150  			nextEnd:   1,
   151  			last:      15,
   152  		},
   153  		{
   154  			initial:   [][2]uint64{{5, 15}},
   155  			start:     2,
   156  			end:       3,
   157  			expected:  "[[2 3] [5 15]]",
   158  			nextStart: 0,
   159  			nextEnd:   1,
   160  			last:      15,
   161  		},
   162  		{
   163  			initial:   [][2]uint64{{5, 15}},
   164  			start:     2,
   165  			end:       4,
   166  			expected:  "[[2 15]]",
   167  			nextStart: 0,
   168  			nextEnd:   1,
   169  			last:      15,
   170  		},
   171  		{
   172  			initial:   [][2]uint64{{0, 1}, {5, 15}},
   173  			start:     2,
   174  			end:       4,
   175  			expected:  "[[0 15]]",
   176  			nextStart: 16,
   177  			nextEnd:   0,
   178  			last:      15,
   179  		},
   180  		{
   181  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   182  			start:     2,
   183  			end:       10,
   184  			expected:  "[[0 10] [15 20]]",
   185  			nextStart: 11,
   186  			nextEnd:   14,
   187  			last:      20,
   188  		},
   189  		{
   190  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   191  			start:     8,
   192  			end:       18,
   193  			expected:  "[[0 5] [8 20]]",
   194  			nextStart: 6,
   195  			nextEnd:   7,
   196  			last:      20,
   197  		},
   198  		{
   199  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   200  			start:     2,
   201  			end:       17,
   202  			expected:  "[[0 20]]",
   203  			nextStart: 21,
   204  			nextEnd:   0,
   205  			last:      20,
   206  		},
   207  		{
   208  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   209  			start:     2,
   210  			end:       25,
   211  			expected:  "[[0 25]]",
   212  			nextStart: 26,
   213  			nextEnd:   0,
   214  			last:      25,
   215  		},
   216  		{
   217  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   218  			start:     5,
   219  			end:       14,
   220  			expected:  "[[0 20]]",
   221  			nextStart: 21,
   222  			nextEnd:   0,
   223  			last:      20,
   224  		},
   225  		{
   226  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   227  			start:     6,
   228  			end:       14,
   229  			expected:  "[[0 20]]",
   230  			nextStart: 21,
   231  			nextEnd:   0,
   232  			last:      20,
   233  		},
   234  		{
   235  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}},
   236  			start:     6,
   237  			end:       29,
   238  			expected:  "[[0 40]]",
   239  			nextStart: 41,
   240  			nextEnd:   0,
   241  			last:      40,
   242  		},
   243  		{
   244  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   245  			start:     3,
   246  			end:       55,
   247  			expected:  "[[0 60]]",
   248  			nextStart: 61,
   249  			nextEnd:   0,
   250  			last:      60,
   251  		},
   252  		{
   253  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   254  			start:     21,
   255  			end:       49,
   256  			expected:  "[[0 5] [15 60]]",
   257  			nextStart: 6,
   258  			nextEnd:   14,
   259  			last:      60,
   260  		},
   261  		{
   262  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   263  			start:     0,
   264  			end:       100,
   265  			expected:  "[[0 100]]",
   266  			nextStart: 101,
   267  			nextEnd:   0,
   268  			last:      100,
   269  		},
   270  		{
   271  			startLimit: 100,
   272  			initial:    nil,
   273  			start:      0,
   274  			end:        0,
   275  			expected:   "[]",
   276  			nextStart:  100,
   277  			nextEnd:    0,
   278  			last:       0,
   279  		},
   280  		{
   281  			startLimit: 100,
   282  			initial:    nil,
   283  			start:      20,
   284  			end:        30,
   285  			expected:   "[]",
   286  			nextStart:  100,
   287  			nextEnd:    0,
   288  			last:       0,
   289  		},
   290  		{
   291  			startLimit: 100,
   292  			initial:    nil,
   293  			start:      50,
   294  			end:        100,
   295  			expected:   "[[100 100]]",
   296  			nextStart:  101,
   297  			nextEnd:    0,
   298  			last:       100,
   299  		},
   300  		{
   301  			startLimit: 100,
   302  			initial:    nil,
   303  			start:      50,
   304  			end:        110,
   305  			expected:   "[[100 110]]",
   306  			nextStart:  111,
   307  			nextEnd:    0,
   308  			last:       110,
   309  		},
   310  		{
   311  			startLimit: 100,
   312  			initial:    nil,
   313  			start:      120,
   314  			end:        130,
   315  			expected:   "[[120 130]]",
   316  			nextStart:  100,
   317  			nextEnd:    119,
   318  			last:       130,
   319  		},
   320  		{
   321  			startLimit: 100,
   322  			initial:    nil,
   323  			start:      120,
   324  			end:        130,
   325  			expected:   "[[120 130]]",
   326  			nextStart:  100,
   327  			nextEnd:    119,
   328  			last:       130,
   329  		},
   330  	} {
   331  		intervals := NewIntervals(tc.startLimit)
   332  		intervals.ranges = tc.initial
   333  		intervals.Add(tc.start, tc.end)
   334  		got := intervals.String()
   335  		if got != tc.expected {
   336  			t.Errorf("interval #%d: expected %s, got %s", i, tc.expected, got)
   337  		}
   338  		nextStart, nextEnd := intervals.Next()
   339  		if nextStart != tc.nextStart {
   340  			t.Errorf("interval #%d, expected next start %d, got %d", i, tc.nextStart, nextStart)
   341  		}
   342  		if nextEnd != tc.nextEnd {
   343  			t.Errorf("interval #%d, expected next end %d, got %d", i, tc.nextEnd, nextEnd)
   344  		}
   345  		last := intervals.Last()
   346  		if last != tc.last {
   347  			t.Errorf("interval #%d, expected last %d, got %d", i, tc.last, last)
   348  		}
   349  	}
   350  }
   351  
   352  func TestMerge(t *testing.T) {
   353  	for i, tc := range []struct {
   354  		initial  [][2]uint64
   355  		merge    [][2]uint64
   356  		expected string
   357  	}{
   358  		{
   359  			initial:  nil,
   360  			merge:    nil,
   361  			expected: "[]",
   362  		},
   363  		{
   364  			initial:  [][2]uint64{{10, 20}},
   365  			merge:    nil,
   366  			expected: "[[10 20]]",
   367  		},
   368  		{
   369  			initial:  nil,
   370  			merge:    [][2]uint64{{15, 25}},
   371  			expected: "[[15 25]]",
   372  		},
   373  		{
   374  			initial:  [][2]uint64{{0, 100}},
   375  			merge:    [][2]uint64{{150, 250}},
   376  			expected: "[[0 100] [150 250]]",
   377  		},
   378  		{
   379  			initial:  [][2]uint64{{0, 100}},
   380  			merge:    [][2]uint64{{101, 250}},
   381  			expected: "[[0 250]]",
   382  		},
   383  		{
   384  			initial:  [][2]uint64{{0, 10}, {30, 40}},
   385  			merge:    [][2]uint64{{20, 25}, {41, 50}},
   386  			expected: "[[0 10] [20 25] [30 50]]",
   387  		},
   388  		{
   389  			initial:  [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   390  			merge:    [][2]uint64{{6, 25}},
   391  			expected: "[[0 25] [30 40] [50 60]]",
   392  		},
   393  	} {
   394  		intervals := NewIntervals(0)
   395  		intervals.ranges = tc.initial
   396  		m := NewIntervals(0)
   397  		m.ranges = tc.merge
   398  
   399  		intervals.Merge(m)
   400  
   401  		got := intervals.String()
   402  		if got != tc.expected {
   403  			t.Errorf("interval #%d: expected %s, got %s", i, tc.expected, got)
   404  		}
   405  	}
   406  }
   407