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