Skip to content

Commit

Permalink
fix: fix negative max edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Nov 26, 2024
1 parent fd2a1e0 commit 43391b1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,9 @@ func (s *programState) sendAll(source parser.Source) (*big.Int, InterpreterError
if err != nil {
return nil, err
}

if monetary.Cmp(big.NewInt(0)) == -1 {
monetary.Set(big.NewInt(0))
}
// We switch to the default sending evaluation for this subsource
return s.trySendingUpTo(source.From, monetary)

Expand Down Expand Up @@ -562,6 +564,9 @@ func (s *programState) trySendingUpTo(source parser.Source, amount *big.Int) (*b
return nil, err
}
cappedAmount := utils.MinBigInt(amount, cap)
if cappedAmount.Cmp(big.NewInt(0)) == -1 {
cappedAmount.Set(big.NewInt(0))
}
return s.trySendingUpTo(source.From, cappedAmount)

default:
Expand Down
49 changes: 49 additions & 0 deletions internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,55 @@ func TestSendAlltMaxWhenNoAmount(t *testing.T) {
test(t, tc)
}

func TestNegativeMaxSendAll(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [USD/2 *] (
source = max [USD/2 -50] from @src
destination = @dest
)
`)
tc.setBalance("src", "USD/2", 0)
tc.expected = CaseResult{
Postings: []Posting{
// Posting omitted
// {
// Asset: "USD/2",
// Amount: big.NewInt(0),
// Source: "src",
// Destination: "dest",
// },
},
Error: nil,
}
test(t, tc)
}

func TestNegativeMax(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [USD/2 100] (
source = {
max [USD/2 -50] from @src
@world
}
destination = @dest
)
`)
tc.setBalance("src", "USD/2", 0)
tc.expected = CaseResult{
Postings: []Posting{

{
Asset: "USD/2",
Amount: big.NewInt(100),
Source: "world",
Destination: "dest",
},
},
Error: nil,
}
test(t, tc)
}

func TestSendAllDestinatioAllot(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [USD/2 *] (
Expand Down

0 comments on commit 43391b1

Please sign in to comment.