Example #1
0
func TestDecGobEncoding(t *testing.T) {
	var medium bytes.Buffer
	enc := gob.NewEncoder(&medium)
	dec := gob.NewDecoder(&medium)
	for i, test := range decGobEncodingTests {
		for j := 0; j < 2; j++ {
			for k := inf.Scale(-5); k <= 5; k++ {
				medium.Reset() // empty buffer for each test case (in case of failures)
				stest := test
				if j != 0 {
					// negative numbers
					stest = "-" + test
				}
				var tx inf.Dec
				tx.SetString(stest)
				tx.SetScale(k) // test with positive, negative, and zero scale
				if err := enc.Encode(&tx); err != nil {
					t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
				}
				var rx inf.Dec
				if err := dec.Decode(&rx); err != nil {
					t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
				}
				if rx.Cmp(&tx) != 0 {
					t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
				}
			}
		}
	}
}
Example #2
0
func TestDecGetString(t *testing.T) {
	z := new(inf.Dec)
	for i, test := range decStringTests {
		if !test.ok {
			continue
		}
		z.SetUnscaled(test.val)
		z.SetScale(test.scale)

		s := z.String()
		if s != test.out {
			t.Errorf("#%da got %s; want %s", i, s, test.out)
		}

		s = fmt.Sprintf("%d", z)
		if s != test.out {
			t.Errorf("#%db got %s; want %s", i, s, test.out)
		}
	}
}