Commit f7202216 authored by zhanghuiren's avatar zhanghuiren

comments & invalid testcase

parent d4151e69
......@@ -18,7 +18,7 @@
package gxbig
import (
"errors"
"fmt"
"math/big"
)
......@@ -30,25 +30,37 @@ type Integer struct {
Value string
}
func (Integer) JavaClassName() string {
func (i *Integer) JavaClassName() string {
return "java.math.BigInteger"
}
// FromString set data from a 10-bases number
func (i *Integer) FromString(s string) error {
intPtr, ok := i.Int.SetString(s, 10)
if !ok || intPtr == nil {
return errors.New(``)
return fmt.Errorf("'%s' is not a 10-based number", s)
}
i.Int = *intPtr
return nil
}
// FromBytes set data from a 10-bases number bytes
func (i *Integer) FromBytes(bytes []byte) error {
i.Int = *i.Int.SetBytes(bytes)
return nil
}
// GetBigInt getter
func (i *Integer) GetBigInt() big.Int {
return i.Int
}
// SetBigInt setter
func (i *Integer) SetBigInt(bigInt big.Int) {
i.Int = bigInt
}
func (i *Integer) String() string {
return i.Int.String()
}
......@@ -21,28 +21,33 @@ import (
"testing"
)
func TestInteger_FromString(t *testing.T) {
type args struct {
s string
}
func TestInteger(t *testing.T) {
tests := []struct {
name string
args args
wantErr bool
name string
src string
wantString string
wantErr bool
}{
{`ten`, args{`10`}, false},
{`-ten`, args{`-10`}, false},
{`30digits`, args{`123456789012345678901234567890`}, false},
{`ten`, `10`, `10`, false},
{`-ten`, `-10`, `-10`, false},
{`30digits`, `123456789012345678901234567890`, `123456789012345678901234567890`, false},
{`invalid-x010`, `x010`, ``, true},
{`invalid-a010`, `a010`, ``, true},
{`invalid-10x`, `10x`, ``, true},
{`invalid-010x`, `010x`, ``, true},
{`special-010`, `010`, `10`, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &Integer{}
if err := i.FromString(tt.args.s); (err != nil) != tt.wantErr {
err := i.FromString(tt.src)
if (err != nil) != tt.wantErr {
t.Errorf("FromString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got := i.String(); got != tt.args.s {
t.Errorf("String() = %v, want %v", got, tt.args.s)
if err == nil && i.String() != tt.wantString {
t.Errorf("String() got %v, want %v", i.String(), tt.wantString)
}
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment