diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-22 12:29:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-22 12:29:40 +0300 |
| commit | cf855e1d0aa037a14abc0a889a5970cadca9805c (patch) | |
| tree | 7952848623c7186a22245ba9baf10687bf890477 /internal | |
| parent | 28c911caa58f8dda05f97b77ea8b47f26610052e (diff) | |
fix(rpn): propagate prefixMode from RPN.SetPrefixMode to Operations; clarify test name
- RPN.SetPrefixMode() now calls ops.SetPrefixMode() to prevent
desync when called from outside evaluate() (where r.mu is held
and ops.SetPrefixMode is called directly)
- Rename TestPrefixModeAffectsArithmetic to
TestPrefixModeUsedInArithmetic and add explanatory comments
(same-metric addition yields same result in both modes — factors
cancel out; other tests verify mode-dependent results)
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/rpn/operations_metric_cmd_test.go | 13 | ||||
| -rw-r--r-- | internal/rpn/rpn_state.go | 2 |
2 files changed, 11 insertions, 4 deletions
diff --git a/internal/rpn/operations_metric_cmd_test.go b/internal/rpn/operations_metric_cmd_test.go index 6e5b422..6bc1ee9 100644 --- a/internal/rpn/operations_metric_cmd_test.go +++ b/internal/rpn/operations_metric_cmd_test.go @@ -312,8 +312,14 @@ func TestPrefixModeAffectsCrossMetricConvert(t *testing.T) { } } -func TestPrefixModeAffectsArithmetic(t *testing.T) { - // SI mode: 1024KB + 1KB in SI = 1024*8000 + 8000 = 8232800 bits → /8000 = 1025KB +func TestPrefixModeUsedInArithmetic(t *testing.T) { + // Same-metric addition: result is the same in both modes + // (conversion factors cancel out when input and output metrics match) + // This test verifies that GetPrefixMode() is called during arithmetic, + // not that SI vs IEC produces different results. + // For mode-dependent results, see TestPrefixModeEndToEndConvert. + + // SI mode: 1024KB + 1KB → 1025KB vars := NewVariables() rpn := NewRPN(vars) result, err := rpn.ParseAndEvaluate("1024KB 1KB + @KB convert") @@ -325,8 +331,7 @@ func TestPrefixModeAffectsArithmetic(t *testing.T) { t.Errorf("SI: 1024KB+1KB→KB = %g, want 1025", resultVal) } - // IEC mode: 1024KB + 1KB where KB = 8*1024 bits - // 1024*8192 + 8192 = 8401408 bits → /8192 = 1025KB + // IEC mode: same result (factors cancel) vars2 := NewVariables() rpn2 := NewRPN(vars2) _, err = rpn2.ParseAndEvaluate("metric binary set") diff --git a/internal/rpn/rpn_state.go b/internal/rpn/rpn_state.go index 1d74e58..3b63984 100644 --- a/internal/rpn/rpn_state.go +++ b/internal/rpn/rpn_state.go @@ -96,11 +96,13 @@ func (r *RPN) Stack() []Number { } // SetPrefixMode sets the prefix mode (SI or IEC). +// Propagates to the Operations instance so arithmetic uses the correct mode. // This method is thread-safe for writes. func (r *RPN) SetPrefixMode(mode PrefixMode) { r.mu.Lock() defer r.mu.Unlock() r.prefixMode = mode + r.ops.SetPrefixMode(mode) } // GetPrefixMode returns the current prefix mode. |
