Commit 791cf3d3 authored by zhanghuiren's avatar zhanghuiren

gxbix.Integer

parent 7368eb97
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxbig
import (
"errors"
"math/big"
)
// Integer represents a integer value.
type Integer struct {
big.Int
// for hessian
Value string
}
func (Integer) JavaClassName() string {
return "java.math.BigInteger"
}
func (i *Integer) FromString(s string) error {
intPtr, ok := i.Int.SetString(s, 10)
if !ok || intPtr == nil {
return errors.New(``)
}
i.Int = *intPtr
return nil
}
func (i *Integer) FromBytes(bytes []byte) error {
i.Int = *i.Int.SetBytes(bytes)
return nil
}
func (i *Integer) String() string {
return i.Int.String()
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gxbig
import (
"testing"
)
func TestInteger_FromString(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
wantErr bool
}{
{`ten`, args{`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 {
t.Errorf("FromString() error = %v, wantErr %v", err, tt.wantErr)
}
if got := i.String(); got != tt.args.s {
t.Errorf("String() = %v, want %v", got, tt.args.s)
}
})
}
}
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