github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/stream/intervals/intervals_test.go (about)

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