decred.org/dcrdex@v1.0.5/server/db/driver/pg/system_test.go (about)

     1  package pg
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestParseUnit(t *testing.T) {
     8  	tests := []struct {
     9  		in   string
    10  		mult float64
    11  		base string
    12  		fail bool
    13  	}{
    14  		{"1", 1.0, "", false},                        // no base unit
    15  		{" ", 1.0, "", false},                        // white space
    16  		{"8kB", 8.0, "kB", false},                    // basic
    17  		{"8 kB ", 8.0, "kB", false},                  // spaces discarded
    18  		{"kB", 1.0, "kB", false},                     // no numeric part
    19  		{".8kB", 0.8, "kB", false},                   // decimal w/ no int
    20  		{"122B", 122.0, "B", false},                  // different base unit
    21  		{"-400MB", -400.0, "MB", false},              // negative
    22  		{".kB", 0.0, "", true},                       // invalid numeric part
    23  		{"1.1.1kB", 0.0, "", true},                   // invalid numeric part
    24  		{"8kB63", 8.0, "kB63", false},                // number in base unit
    25  		{"1.21 GW", 1.21, "GW", false},               // strip space between number and base unit
    26  		{"J/s", 1.0, "J/s", false},                   // complex base unit
    27  		{"kg m^2 / s^2", 1.0, "kg m^2 / s^2", false}, // complex base unit
    28  		{"-v", -1.0, "v", false},                     // consider a lone dash prefix as a negation
    29  		{"7 dog years", 7.0, "dog years", false},     // base unit with spaces
    30  		{"", 1.0, "", false},                         // empty string
    31  	}
    32  
    33  	for i := range tests {
    34  		ti := &tests[i]
    35  		mult, base, err := parseUnit(ti.in)
    36  		if err != nil && !ti.fail {
    37  			t.Errorf("parseUnit(%s) failed: %v", ti.in, err)
    38  		} else if err == nil && ti.fail {
    39  			t.Errorf("parseUnit(%s) was supposed to fail.", ti.in)
    40  		}
    41  		if mult != ti.mult {
    42  			t.Errorf("parseUnit(%s) returned mult %f, expected %f", ti.in, mult, ti.mult)
    43  		}
    44  		if base != ti.base {
    45  			t.Errorf("parseUnit(%s) returned base %s, expected %s", ti.in, base, ti.base)
    46  		}
    47  		//t.Logf("multiple=%f, base=%s", mult, base)
    48  	}
    49  
    50  }