github.com/dotlike13/wemix30_go@v1.8.23/swarm/network/stream/intervals/intervals_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package intervals
    18  
    19  import "testing"
    20  
    21  // Test tests Interval methods Add, Next and Last for various
    22  // initial state.
    23  func Test(t *testing.T) {
    24  	for i, tc := range []struct {
    25  		startLimit uint64
    26  		initial    [][2]uint64
    27  		start      uint64
    28  		end        uint64
    29  		expected   string
    30  		nextStart  uint64
    31  		nextEnd    uint64
    32  		last       uint64
    33  	}{
    34  		{
    35  			initial:   nil,
    36  			start:     0,
    37  			end:       0,
    38  			expected:  "[[0 0]]",
    39  			nextStart: 1,
    40  			nextEnd:   0,
    41  			last:      0,
    42  		},
    43  		{
    44  			initial:   nil,
    45  			start:     0,
    46  			end:       10,
    47  			expected:  "[[0 10]]",
    48  			nextStart: 11,
    49  			nextEnd:   0,
    50  			last:      10,
    51  		},
    52  		{
    53  			initial:   nil,
    54  			start:     5,
    55  			end:       15,
    56  			expected:  "[[5 15]]",
    57  			nextStart: 0,
    58  			nextEnd:   4,
    59  			last:      15,
    60  		},
    61  		{
    62  			initial:   [][2]uint64{{0, 0}},
    63  			start:     0,
    64  			end:       0,
    65  			expected:  "[[0 0]]",
    66  			nextStart: 1,
    67  			nextEnd:   0,
    68  			last:      0,
    69  		},
    70  		{
    71  			initial:   [][2]uint64{{0, 0}},
    72  			start:     5,
    73  			end:       15,
    74  			expected:  "[[0 0] [5 15]]",
    75  			nextStart: 1,
    76  			nextEnd:   4,
    77  			last:      15,
    78  		},
    79  		{
    80  			initial:   [][2]uint64{{5, 15}},
    81  			start:     5,
    82  			end:       15,
    83  			expected:  "[[5 15]]",
    84  			nextStart: 0,
    85  			nextEnd:   4,
    86  			last:      15,
    87  		},
    88  		{
    89  			initial:   [][2]uint64{{5, 15}},
    90  			start:     5,
    91  			end:       20,
    92  			expected:  "[[5 20]]",
    93  			nextStart: 0,
    94  			nextEnd:   4,
    95  			last:      20,
    96  		},
    97  		{
    98  			initial:   [][2]uint64{{5, 15}},
    99  			start:     10,
   100  			end:       20,
   101  			expected:  "[[5 20]]",
   102  			nextStart: 0,
   103  			nextEnd:   4,
   104  			last:      20,
   105  		},
   106  		{
   107  			initial:   [][2]uint64{{5, 15}},
   108  			start:     0,
   109  			end:       20,
   110  			expected:  "[[0 20]]",
   111  			nextStart: 21,
   112  			nextEnd:   0,
   113  			last:      20,
   114  		},
   115  		{
   116  			initial:   [][2]uint64{{5, 15}},
   117  			start:     2,
   118  			end:       10,
   119  			expected:  "[[2 15]]",
   120  			nextStart: 0,
   121  			nextEnd:   1,
   122  			last:      15,
   123  		},
   124  		{
   125  			initial:   [][2]uint64{{5, 15}},
   126  			start:     2,
   127  			end:       4,
   128  			expected:  "[[2 15]]",
   129  			nextStart: 0,
   130  			nextEnd:   1,
   131  			last:      15,
   132  		},
   133  		{
   134  			initial:   [][2]uint64{{5, 15}},
   135  			start:     2,
   136  			end:       5,
   137  			expected:  "[[2 15]]",
   138  			nextStart: 0,
   139  			nextEnd:   1,
   140  			last:      15,
   141  		},
   142  		{
   143  			initial:   [][2]uint64{{5, 15}},
   144  			start:     2,
   145  			end:       3,
   146  			expected:  "[[2 3] [5 15]]",
   147  			nextStart: 0,
   148  			nextEnd:   1,
   149  			last:      15,
   150  		},
   151  		{
   152  			initial:   [][2]uint64{{5, 15}},
   153  			start:     2,
   154  			end:       4,
   155  			expected:  "[[2 15]]",
   156  			nextStart: 0,
   157  			nextEnd:   1,
   158  			last:      15,
   159  		},
   160  		{
   161  			initial:   [][2]uint64{{0, 1}, {5, 15}},
   162  			start:     2,
   163  			end:       4,
   164  			expected:  "[[0 15]]",
   165  			nextStart: 16,
   166  			nextEnd:   0,
   167  			last:      15,
   168  		},
   169  		{
   170  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   171  			start:     2,
   172  			end:       10,
   173  			expected:  "[[0 10] [15 20]]",
   174  			nextStart: 11,
   175  			nextEnd:   14,
   176  			last:      20,
   177  		},
   178  		{
   179  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   180  			start:     8,
   181  			end:       18,
   182  			expected:  "[[0 5] [8 20]]",
   183  			nextStart: 6,
   184  			nextEnd:   7,
   185  			last:      20,
   186  		},
   187  		{
   188  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   189  			start:     2,
   190  			end:       17,
   191  			expected:  "[[0 20]]",
   192  			nextStart: 21,
   193  			nextEnd:   0,
   194  			last:      20,
   195  		},
   196  		{
   197  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   198  			start:     2,
   199  			end:       25,
   200  			expected:  "[[0 25]]",
   201  			nextStart: 26,
   202  			nextEnd:   0,
   203  			last:      25,
   204  		},
   205  		{
   206  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   207  			start:     5,
   208  			end:       14,
   209  			expected:  "[[0 20]]",
   210  			nextStart: 21,
   211  			nextEnd:   0,
   212  			last:      20,
   213  		},
   214  		{
   215  			initial:   [][2]uint64{{0, 5}, {15, 20}},
   216  			start:     6,
   217  			end:       14,
   218  			expected:  "[[0 20]]",
   219  			nextStart: 21,
   220  			nextEnd:   0,
   221  			last:      20,
   222  		},
   223  		{
   224  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}},
   225  			start:     6,
   226  			end:       29,
   227  			expected:  "[[0 40]]",
   228  			nextStart: 41,
   229  			nextEnd:   0,
   230  			last:      40,
   231  		},
   232  		{
   233  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   234  			start:     3,
   235  			end:       55,
   236  			expected:  "[[0 60]]",
   237  			nextStart: 61,
   238  			nextEnd:   0,
   239  			last:      60,
   240  		},
   241  		{
   242  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   243  			start:     21,
   244  			end:       49,
   245  			expected:  "[[0 5] [15 60]]",
   246  			nextStart: 6,
   247  			nextEnd:   14,
   248  			last:      60,
   249  		},
   250  		{
   251  			initial:   [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   252  			start:     0,
   253  			end:       100,
   254  			expected:  "[[0 100]]",
   255  			nextStart: 101,
   256  			nextEnd:   0,
   257  			last:      100,
   258  		},
   259  		{
   260  			startLimit: 100,
   261  			initial:    nil,
   262  			start:      0,
   263  			end:        0,
   264  			expected:   "[]",
   265  			nextStart:  100,
   266  			nextEnd:    0,
   267  			last:       0,
   268  		},
   269  		{
   270  			startLimit: 100,
   271  			initial:    nil,
   272  			start:      20,
   273  			end:        30,
   274  			expected:   "[]",
   275  			nextStart:  100,
   276  			nextEnd:    0,
   277  			last:       0,
   278  		},
   279  		{
   280  			startLimit: 100,
   281  			initial:    nil,
   282  			start:      50,
   283  			end:        100,
   284  			expected:   "[[100 100]]",
   285  			nextStart:  101,
   286  			nextEnd:    0,
   287  			last:       100,
   288  		},
   289  		{
   290  			startLimit: 100,
   291  			initial:    nil,
   292  			start:      50,
   293  			end:        110,
   294  			expected:   "[[100 110]]",
   295  			nextStart:  111,
   296  			nextEnd:    0,
   297  			last:       110,
   298  		},
   299  		{
   300  			startLimit: 100,
   301  			initial:    nil,
   302  			start:      120,
   303  			end:        130,
   304  			expected:   "[[120 130]]",
   305  			nextStart:  100,
   306  			nextEnd:    119,
   307  			last:       130,
   308  		},
   309  		{
   310  			startLimit: 100,
   311  			initial:    nil,
   312  			start:      120,
   313  			end:        130,
   314  			expected:   "[[120 130]]",
   315  			nextStart:  100,
   316  			nextEnd:    119,
   317  			last:       130,
   318  		},
   319  	} {
   320  		intervals := NewIntervals(tc.startLimit)
   321  		intervals.ranges = tc.initial
   322  		intervals.Add(tc.start, tc.end)
   323  		got := intervals.String()
   324  		if got != tc.expected {
   325  			t.Errorf("interval #%d: expected %s, got %s", i, tc.expected, got)
   326  		}
   327  		nextStart, nextEnd := intervals.Next()
   328  		if nextStart != tc.nextStart {
   329  			t.Errorf("interval #%d, expected next start %d, got %d", i, tc.nextStart, nextStart)
   330  		}
   331  		if nextEnd != tc.nextEnd {
   332  			t.Errorf("interval #%d, expected next end %d, got %d", i, tc.nextEnd, nextEnd)
   333  		}
   334  		last := intervals.Last()
   335  		if last != tc.last {
   336  			t.Errorf("interval #%d, expected last %d, got %d", i, tc.last, last)
   337  		}
   338  	}
   339  }
   340  
   341  func TestMerge(t *testing.T) {
   342  	for i, tc := range []struct {
   343  		initial  [][2]uint64
   344  		merge    [][2]uint64
   345  		expected string
   346  	}{
   347  		{
   348  			initial:  nil,
   349  			merge:    nil,
   350  			expected: "[]",
   351  		},
   352  		{
   353  			initial:  [][2]uint64{{10, 20}},
   354  			merge:    nil,
   355  			expected: "[[10 20]]",
   356  		},
   357  		{
   358  			initial:  nil,
   359  			merge:    [][2]uint64{{15, 25}},
   360  			expected: "[[15 25]]",
   361  		},
   362  		{
   363  			initial:  [][2]uint64{{0, 100}},
   364  			merge:    [][2]uint64{{150, 250}},
   365  			expected: "[[0 100] [150 250]]",
   366  		},
   367  		{
   368  			initial:  [][2]uint64{{0, 100}},
   369  			merge:    [][2]uint64{{101, 250}},
   370  			expected: "[[0 250]]",
   371  		},
   372  		{
   373  			initial:  [][2]uint64{{0, 10}, {30, 40}},
   374  			merge:    [][2]uint64{{20, 25}, {41, 50}},
   375  			expected: "[[0 10] [20 25] [30 50]]",
   376  		},
   377  		{
   378  			initial:  [][2]uint64{{0, 5}, {15, 20}, {30, 40}, {50, 60}},
   379  			merge:    [][2]uint64{{6, 25}},
   380  			expected: "[[0 25] [30 40] [50 60]]",
   381  		},
   382  	} {
   383  		intervals := NewIntervals(0)
   384  		intervals.ranges = tc.initial
   385  		m := NewIntervals(0)
   386  		m.ranges = tc.merge
   387  
   388  		intervals.Merge(m)
   389  
   390  		got := intervals.String()
   391  		if got != tc.expected {
   392  			t.Errorf("interval #%d: expected %s, got %s", i, tc.expected, got)
   393  		}
   394  	}
   395  }