golang.org/x/build@v0.0.0-20240506185731-218518f32b70/devapp/stats_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  	"time"
    11  
    12  	"golang.org/x/build/maintner"
    13  )
    14  
    15  func TestNewIntervalFromCL(t *testing.T) {
    16  	var (
    17  		t0 = time.Now()
    18  		t1 = t0.Add(1 * time.Hour)
    19  	)
    20  	testCases := []struct {
    21  		cl         *maintner.GerritCL
    22  		start, end int64
    23  	}{
    24  		{
    25  			cl: &maintner.GerritCL{
    26  				Created: t0,
    27  				Status:  "new",
    28  			},
    29  			start: t0.Unix(),
    30  			end:   math.MaxInt64,
    31  		},
    32  		{
    33  			cl: &maintner.GerritCL{
    34  				Created: t0,
    35  				Status:  "merged",
    36  				Metas: []*maintner.GerritMeta{
    37  					{
    38  						Commit: &maintner.GitCommit{
    39  							Msg:        "autogenerated:gerrit:merged",
    40  							CommitTime: t1,
    41  						},
    42  					},
    43  				},
    44  			},
    45  			start: t0.Unix(),
    46  			end:   t1.Unix(),
    47  		},
    48  		{
    49  			cl: &maintner.GerritCL{
    50  				Created: t0,
    51  				Status:  "abandoned",
    52  				Metas: []*maintner.GerritMeta{
    53  					{
    54  						Commit: &maintner.GitCommit{
    55  							Msg:        "autogenerated:gerrit:abandon",
    56  							CommitTime: t1,
    57  						},
    58  					},
    59  				},
    60  			},
    61  			start: t0.Unix(),
    62  			end:   t1.Unix(),
    63  		},
    64  	}
    65  
    66  	for _, tc := range testCases {
    67  		ival := newIntervalFromCL(tc.cl)
    68  		if got, want := ival.start, tc.start; got != want {
    69  			t.Errorf("start: got %d; want %d", got, want)
    70  		}
    71  		if got, want := ival.end, tc.end; got != want {
    72  			t.Errorf("end: got %d; want %d", got, want)
    73  		}
    74  		if got, want := ival.cl, tc.cl; got != want {
    75  			t.Errorf("cl: got %+v; want %+v", got, want)
    76  		}
    77  	}
    78  }
    79  
    80  func TestIntervalIntersection(t *testing.T) {
    81  	testCases := []struct {
    82  		interval   *clInterval
    83  		t0, t1     time.Time
    84  		intersects bool
    85  	}{
    86  		{
    87  			&clInterval{start: 0, end: 5},
    88  			time.Unix(0, 0),
    89  			time.Unix(10, 0),
    90  			true,
    91  		},
    92  		{
    93  			&clInterval{start: 10, end: 20},
    94  			time.Unix(0, 0),
    95  			time.Unix(10, 0),
    96  			true,
    97  		},
    98  		{
    99  			&clInterval{start: 10, end: 20},
   100  			time.Unix(0, 0),
   101  			time.Unix(9, 0),
   102  			false,
   103  		},
   104  		{
   105  			&clInterval{start: 0, end: 5},
   106  			time.Unix(6, 0),
   107  			time.Unix(10, 0),
   108  			false,
   109  		},
   110  	}
   111  
   112  	for _, tc := range testCases {
   113  		if got, want := tc.interval.intersects(tc.t0, tc.t1), tc.intersects; got != want {
   114  			t.Errorf("(%v).intersects(%v, %v): got %v; want %v", tc.interval, tc.t0, tc.t1, got, want)
   115  		}
   116  	}
   117  }