Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the description of Copy() method does not correspond to reality #327

Closed
lexand opened this issue Aug 31, 2023 · 1 comment
Closed

the description of Copy() method does not correspond to reality #327

lexand opened this issue Aug 31, 2023 · 1 comment

Comments

@lexand
Copy link

lexand commented Aug 31, 2023

Method Copy() says // Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.

Got:
Simple test show that pointers are actually equal

func TestDecimal_Copy(t *testing.T) {
	a := decimal.RequireFromString("1")
	b := a.Copy()

	refA := reflect.ValueOf(a)
	refB := reflect.ValueOf(b)

	ptrA := refA.Field(0).Pointer()
	ptrB := refB.Field(0).Pointer()

	assert.NotEqual(t, ptrA, ptrB)
}

Your test for Copy() method does not check inequality of pointers

func TestCopy(t *testing.T) {
	origin := New(1, 0)
	cpy := origin.Copy()

	if cpy.Cmp(origin) != 0 {
		t.Error("expecting copy and origin to be equals, but they are not")
	}

	//change value
	cpy = cpy.Add(New(1, 0))

	if cpy.Cmp(origin) == 0 {
		t.Error("expecting copy and origin to have different values, but they are equal")
	}
}

copy

Expect:
values have different pointers

$ go version
go version go1.20.5 linux/amd64
@mwoss
Copy link
Member

mwoss commented Dec 27, 2023

Thanks for the report! Issue is fixed by #278

@mwoss mwoss closed this as completed Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants