diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-11 22:45:57 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-11 22:45:57 +0300 |
| commit | fe31559af818bcc1771b1a2eafc7f3aa1cac3f75 (patch) | |
| tree | 192545c30e748de5a8bc96bf7d116e5971ee15f7 /integrationtests | |
| parent | a1a55e2f1702b9f0f95ec10d3d9adcf35999f006 (diff) | |
feat: implement binary exponentiation with ** operator
- Add PowInt(int) method to Number interface
- Implement binary exponentiation for Float and Rat types
- Add FastPower operator to handle ** operator
- Register ** operator in NewOperatorRegistry
- Add unit tests for PowInt (basic, large, negative exponents)
- Add integration tests for ** operator (CLI and stdin)
- Update README.md with ** operator documentation
Diffstat (limited to 'integrationtests')
| -rw-r--r-- | integrationtests/cli_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/integrationtests/cli_test.go b/integrationtests/cli_test.go index 8a0a076..fe62185 100644 --- a/integrationtests/cli_test.go +++ b/integrationtests/cli_test.go @@ -544,3 +544,98 @@ func TestCLIVariableAssignmentRepetition(t *testing.T) { }) } } + +// TestCLIBinaryExponentiation tests the ** operator with binary exponentiation. +func TestCLIBinaryExponentiation(t *testing.T) { + binaryPath := buildBinary(t) + + tests := []struct { + name string + args []string + expected string + }{ + { + name: "2 ** 10", + args: []string{"2", "10", "**"}, + expected: "1024", + }, + { + name: "3 ** 4", + args: []string{"3", "4", "**"}, + expected: "81", + }, + { + name: "2 ** -3", + args: []string{"2", "-3", "**"}, + expected: "0.125", + }, + { + name: "10 ** -2", + args: []string{"10", "-2", "**"}, + expected: "0.01", + }, + { + name: "5 ** 0", + args: []string{"5", "0", "**"}, + expected: "1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := exec.Command(binaryPath, tt.args...) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("command failed: %v\nOutput: %s", err, string(output)) + } + + outputStr := strings.TrimSpace(string(output)) + if !strings.Contains(outputStr, tt.expected) { + t.Errorf("output should contain '%s', got: %s", tt.expected, outputStr) + } + }) + } +} + +// TestCLIBinaryExponentiationStdin tests the ** operator via stdin. +func TestCLIBinaryExponentiationStdin(t *testing.T) { + binaryPath := buildBinary(t) + + tests := []struct { + name string + stdin string + expected string + }{ + { + name: "2 ** 10 via stdin", + stdin: "2 10 **", + expected: "1024", + }, + { + name: "3 ** 4 via stdin", + stdin: "3 4 **", + expected: "81", + }, + { + name: "2 ** -3 via stdin", + stdin: "2 -3 **", + expected: "0.125", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := exec.Command(binaryPath) + cmd.Stdin = strings.NewReader(tt.stdin) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("command failed: %v\nOutput: %s", err, string(output)) + } + + outputStr := strings.TrimSpace(string(output)) + if !strings.Contains(outputStr, tt.expected) { + t.Errorf("output should contain '%s', got: %s", tt.expected, outputStr) + } + }) + } +} |
