github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/ptimer/ptimer_test.go (about)

     1  package ptimer
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"regexp"
     8  	"slices"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  const timePattern = "\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d\\d\\d\\d"
    14  
    15  func TestNew(t *testing.T) {
    16  	want := "^"
    17  	for _, i := range []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 35} {
    18  		want += fmt.Sprintf("\r%s \\(%s\\) %d", timePattern, timePattern, i)
    19  	}
    20  	want += "\n$"
    21  
    22  	got := bytes.NewBuffer(nil)
    23  	pt := New()
    24  	pt.W = got
    25  	for i := 0; i < 35; i++ {
    26  		pt.Inc()
    27  	}
    28  	pt.Done()
    29  
    30  	if match, _ := regexp.MatchString(want, got.String()); !match {
    31  		t.Fatalf("Inc()+Done()=%q, want %q", got.String(), want)
    32  	}
    33  }
    34  
    35  func TestNewMessage(t *testing.T) {
    36  	want := "^"
    37  	for _, i := range []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 35} {
    38  		want += fmt.Sprintf("\r%s \\(%s\\) hey %d ho",
    39  			timePattern, timePattern, i)
    40  	}
    41  	want += "\n$"
    42  
    43  	got := bytes.NewBuffer(nil)
    44  	pt := NewMessage("hey {} ho")
    45  	pt.W = got
    46  	for i := 0; i < 35; i++ {
    47  		pt.Inc()
    48  	}
    49  	pt.Done()
    50  
    51  	if match, _ := regexp.MatchString(want, got.String()); !match {
    52  		t.Fatalf("Inc()+Done()=%q, want %q", got.String(), want)
    53  	}
    54  }
    55  
    56  func TestNewFunc(t *testing.T) {
    57  	want := "^"
    58  	for _, i := range []float64{1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5,
    59  		10.5, 20.5, 30.5, 35.5} {
    60  		want += fmt.Sprintf("\r%s \\(%s\\) ho ho %f",
    61  			timePattern, timePattern, i)
    62  	}
    63  	want += "\n$"
    64  
    65  	got := bytes.NewBuffer(nil)
    66  	pt := NewFunc(func(i int) string {
    67  		return fmt.Sprintf("ho ho %f", float64(i)+0.5)
    68  	})
    69  	pt.W = got
    70  	for i := 0; i < 35; i++ {
    71  		pt.Inc()
    72  	}
    73  	pt.Done()
    74  
    75  	if match, _ := regexp.MatchString(want, got.String()); !match {
    76  		t.Fatalf("Inc()+Done()=%q, want %q", got.String(), want)
    77  	}
    78  }
    79  
    80  func TestDone(t *testing.T) {
    81  	want := "^" + timePattern + " hello\n$"
    82  
    83  	got := bytes.NewBuffer(nil)
    84  	pt := NewMessage("hello")
    85  	pt.W = got
    86  	pt.Done()
    87  
    88  	if match, _ := regexp.MatchString(want, got.String()); !match {
    89  		t.Fatalf("Done()=%q, want %q", got.String(), want)
    90  	}
    91  }
    92  
    93  func TestNextCheckpoint(t *testing.T) {
    94  	want := []int{
    95  		1, 2, 3, 4, 5, 6, 7, 8, 9,
    96  		10, 20, 30, 40, 50, 60, 70, 80, 90,
    97  		100, 200, 300, 400, 500}
    98  	var got []int
    99  	i := 0
   100  	for range want {
   101  		i = nextCheckpoint(i)
   102  		got = append(got, i)
   103  	}
   104  	if !slices.Equal(got, want) {
   105  		t.Fatalf("nextCheckpoint(...)=%v, want %v", got, want)
   106  	}
   107  }
   108  
   109  func BenchmarkTimer_inc(b *testing.B) {
   110  	pt := New()
   111  	pt.W = io.Discard
   112  	b.ResetTimer()
   113  	for i := 0; i < b.N; i++ {
   114  		pt.Inc()
   115  	}
   116  }
   117  
   118  func Example() {
   119  	pt := New()
   120  	for i := 0; i < 45; i++ {
   121  		time.Sleep(100 * time.Millisecond)
   122  		pt.Inc()
   123  	}
   124  	pt.Done()
   125  }