bitbucket.org/ai69/amoy@v0.2.3/humanize_test.go (about)

     1  package amoy
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestHumanizeDuration(t *testing.T) {
    10  	tests := []struct {
    11  		name     string
    12  		duration time.Duration
    13  		want     string
    14  	}{
    15  		{"1 Second", 1 * Second, "1 sec"},
    16  		{"2 Seconds", 2 * Second, "2 secs"},
    17  		{"51 Minutes 52 Seconds", 51*Minute + 52*Second, "51 mins 52 secs"},
    18  		{"3 Days 2 Hours 1 Minutes", 3*Day + 2*Hour + 1*Minute, "3 days 2 hours 1 min"},
    19  		{"1 Week", 1 * Week, "7 days"},
    20  	}
    21  	for _, tt := range tests {
    22  		t.Run(tt.name, func(t *testing.T) {
    23  			if got := HumanizeDuration(tt.duration); got != tt.want {
    24  				t.Errorf("HumanizeDuration() = %v, want %v", got, tt.want)
    25  			}
    26  		})
    27  	}
    28  }
    29  
    30  func TestHumanizeBytes(t *testing.T) {
    31  	tests := []struct {
    32  		name string
    33  		s    uint64
    34  		want string
    35  	}{
    36  		{"Sample", 82854982, "83 MB"},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			if got := HumanizeBytes(tt.s); got != tt.want {
    41  				t.Errorf("HumanizeBytes() = %v, wantF %v", got, tt.want)
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func TestHumanizeIBytes(t *testing.T) {
    48  	tests := []struct {
    49  		name string
    50  		s    uint64
    51  		want string
    52  	}{
    53  		{"Sample", 82854982, "79 MiB"},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			if got := HumanizeIBytes(tt.s); got != tt.want {
    58  				t.Errorf("HumanizeIBytes() = %v, wantF %v", got, tt.want)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestParseHumanizedBytes(t *testing.T) {
    65  	tests := []struct {
    66  		name    string
    67  		s       string
    68  		want    uint64
    69  		wantErr bool
    70  	}{
    71  		{"Sample 1", "42 MB", 42000000, false},
    72  		{"Sample 2", "42 mib", 44040192, false},
    73  	}
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			got, err := ParseHumanizedBytes(tt.s)
    77  			if (err != nil) != tt.wantErr {
    78  				t.Errorf("ParseHumanizedBytes() error = %v, wantErr %v", err, tt.wantErr)
    79  				return
    80  			}
    81  			if got != tt.want {
    82  				t.Errorf("ParseHumanizedBytes() got = %v, wantF %v", got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestComputeSI(t *testing.T) {
    89  	tests := []struct {
    90  		name  string
    91  		input float64
    92  		wantF float64
    93  		wantS string
    94  	}{
    95  		{"Sample", 2.2345e-12, 2.2345, "p"},
    96  		{"Attempt", 0.01, 10, "m"},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			gotF, gotS := ComputeSI(tt.input)
   101  			if math.Abs(gotF-tt.wantF) > 1e-6 {
   102  				t.Errorf("ComputeSI() gotF = %v, wantF %v", gotF, tt.wantF)
   103  			}
   104  			if gotS != tt.wantS {
   105  				t.Errorf("ComputeSI() gotS = %v, wantF %v", gotS, tt.wantS)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func TestParseSI(t *testing.T) {
   112  	tests := []struct {
   113  		name    string
   114  		input   string
   115  		wantF   float64
   116  		wantS   string
   117  		wantErr bool
   118  	}{
   119  		{"Sample", "2.2345 pF", 2.2345e-12, "F", false},
   120  	}
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			gotF, gotS, err := ParseSI(tt.input)
   124  			if (err != nil) != tt.wantErr {
   125  				t.Errorf("ParseSI() error = %v, wantErr %v", err, tt.wantErr)
   126  				return
   127  			}
   128  			if math.Abs(gotF-tt.wantF) > 1e-6 {
   129  				t.Errorf("ParseSI() gotF = %v, wantF %v", gotF, tt.wantF)
   130  			}
   131  			if gotS != tt.wantS {
   132  				t.Errorf("ParseSI() gotS = %v, wantF %v", gotS, tt.wantS)
   133  			}
   134  		})
   135  	}
   136  }
   137  
   138  func TestSI(t *testing.T) {
   139  	tests := []struct {
   140  		name  string
   141  		input float64
   142  		unit  string
   143  		want  string
   144  	}{
   145  		{"Sample1", 1000000, "B", "1 MB"},
   146  		{"Sample2", 2.2345e-12, "F", "2.2345 pF"},
   147  		{"Attempt1", 1000, "", "1 k"},
   148  		{"Attempt2", 1024, "", "1.024 k"},
   149  		{"Attempt3", 2300000000, "Hz", "2.3 GHz"},
   150  	}
   151  	for _, tt := range tests {
   152  		t.Run(tt.name, func(t *testing.T) {
   153  			if got := SI(tt.input, tt.unit); got != tt.want {
   154  				t.Errorf("SI() = %v, wantF %v", got, tt.want)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  func TestSIWithDigits(t *testing.T) {
   161  	tests := []struct {
   162  		name     string
   163  		input    float64
   164  		decimals int
   165  		unit     string
   166  		want     string
   167  	}{
   168  		{"Sample1", 1000000, 0, "B", "1 MB"},
   169  		{"Sample2", 2.2345e-12, 2, "F", "2.23 pF"},
   170  	}
   171  	for _, tt := range tests {
   172  		t.Run(tt.name, func(t *testing.T) {
   173  			if got := SIWithDigits(tt.input, tt.decimals, tt.unit); got != tt.want {
   174  				t.Errorf("SIWithDigits() = %v, wantF %v", got, tt.want)
   175  			}
   176  		})
   177  	}
   178  }
   179  
   180  func TestFtoa(t *testing.T) {
   181  	tests := []struct {
   182  		name string
   183  		num  float64
   184  		want string
   185  	}{
   186  		{"Attempt1", 0.123, "0.123"},
   187  		{"Attempt2", 1.000, "1"},
   188  	}
   189  	for _, tt := range tests {
   190  		t.Run(tt.name, func(t *testing.T) {
   191  			if got := Ftoa(tt.num); got != tt.want {
   192  				t.Errorf("Ftoa() = %v, wantF %v", got, tt.want)
   193  			}
   194  		})
   195  	}
   196  }
   197  
   198  func TestFtoaWithDigits(t *testing.T) {
   199  	tests := []struct {
   200  		name   string
   201  		num    float64
   202  		digits int
   203  		want   string
   204  	}{
   205  		{"Attempt1", 0.123, 2, "0.12"},
   206  		{"Attempt2", 1.000, 2, "1"},
   207  	}
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(t *testing.T) {
   210  			if got := FtoaWithDigits(tt.num, tt.digits); got != tt.want {
   211  				t.Errorf("FtoaWithDigits() = %v, wantF %v", got, tt.want)
   212  			}
   213  		})
   214  	}
   215  }