summaryrefslogtreecommitdiff
path: root/internal/rpn/variables_test.go
blob: cb12f40b63e32f012dc8f1fc7bd67a0739be95f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package rpn

import (
	"fmt"
	"strings"
	"testing"
)

func TestNewVariables(t *testing.T) {
	v := NewVariables()
	if v == nil {
		t.Fatal("NewVariables() returned nil")
	}
	if v.Count() != 0 {
		t.Errorf("NewVariables() count = %d, want 0", v.Count())
	}
}

func TestSetVariable(t *testing.T) {
	v := NewVariables()

	tests := []struct {
		name  string
		value float64
	}{
		{"x", 5.0},
		{"pi", 3.14159},
		{"result", 42.0},
		{"alpha", -10.5},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := v.SetVariable(tt.name, tt.value)
			if err != nil {
				t.Fatalf("SetVariable(%q, %v) returned error: %v", tt.name, tt.value, err)
			}

			value, exists := v.GetVariable(tt.name)
			if !exists {
				t.Errorf("Variable %q should exist after SetVariable", tt.name)
			}
			if value != tt.value {
				t.Errorf("GetVariable(%q) = %v, want %v", tt.name, value, tt.value)
			}
		})
	}
}

func TestGetVariable(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("test", 100.0)

	tests := []struct {
		name     string
		value    float64
		expected bool
	}{
		{"test", 100.0, true},
		{"nonexistent", 0, false},
		{"", 0, false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			value, exists := v.GetVariable(tt.name)
			if exists != tt.expected {
				t.Errorf("GetVariable(%q) exists = %v, want %v", tt.name, exists, tt.expected)
			}
			if exists && value != tt.value {
				t.Errorf("GetVariable(%q) = %v, want %v", tt.name, value, tt.value)
			}
		})
	}
}

func TestDeleteVariable(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("temp", 50.0)

	// Delete existing variable
	deleted := v.DeleteVariable("temp")
	if !deleted {
		t.Error("DeleteVariable(\"temp\") should return true for existing variable")
	}
	if v.Count() != 0 {
		t.Errorf("Count after delete = %d, want 0", v.Count())
	}

	// Delete non-existent variable
	deleted = v.DeleteVariable("nonexistent")
	if deleted {
		t.Error("DeleteVariable(\"nonexistent\") should return false for non-existent variable")
	}
}

func TestListVariables(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("z", 3.0)
	_ = v.SetVariable("a", 1.0)
	_ = v.SetVariable("m", 2.0)

	infos := v.ListVariables()

	if len(infos) != 3 {
		t.Errorf("ListVariables() returned %d variables, want 3", len(infos))
	}

	// Verify sorted order
	expectedOrder := []string{"a", "m", "z"}
	for i, info := range infos {
		if info.Name != expectedOrder[i] {
			t.Errorf("ListVariables()[%d] name = %q, want %q", i, info.Name, expectedOrder[i])
		}
	}
}

func TestClearVariables(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("x", 1.0)
	_ = v.SetVariable("y", 2.0)
	_ = v.SetVariable("z", 3.0)

	v.ClearVariables()

	if v.Count() != 0 {
		t.Errorf("Count after ClearVariables() = %d, want 0", v.Count())
	}

	// Verify all variables are gone
	_, exists := v.GetVariable("x")
	if exists {
		t.Error("Variable x should not exist after ClearVariables()")
	}
}

func TestFormatVariables(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("pi", 3.14159)
	_ = v.SetVariable("e", 2.71828)

	formatted := v.FormatVariables()

	if strings.Contains(formatted, "No variables defined") {
		t.Error("Formatted variables should not show 'No variables defined' when variables exist")
	}

	if !strings.Contains(formatted, "pi") || !strings.Contains(formatted, "e") {
		t.Errorf("Formatted variables should contain all variable names, got: %s", formatted)
	}
}

func TestFormatVariablesEmpty(t *testing.T) {
	v := NewVariables()
	formatted := v.FormatVariables()

	if !strings.Contains(formatted, "No variables defined") {
		t.Errorf("Empty variables should show 'No variables defined', got: %s", formatted)
	}
}

func TestHasVariable(t *testing.T) {
	v := NewVariables()
	_ = v.SetVariable("exists", 1.0)

	tests := []struct {
		name     string
		expected bool
	}{
		{"exists", true},
		{"nonexistent", false},
		{"", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if v.HasVariable(tt.name) != tt.expected {
				t.Errorf("HasVariable(%q) = %v, want %v", tt.name, v.HasVariable(tt.name), tt.expected)
			}
		})
	}
}

func TestErrVariableNotFound(t *testing.T) {
	v := NewVariables()
	_, exists := v.GetVariable("nonexistent")
	if exists {
		t.Error("GetVariable on non-existent variable should return exists=false")
	}
}

func TestErrInvalidVariableName(t *testing.T) {
	v := NewVariables()
	err := v.SetVariable("", 1.0)
	if err != ErrInvalidVariableName {
		t.Errorf("SetVariable with empty name should return ErrInvalidVariableName, got %v", err)
	}
}

func TestVariableConcurrency(t *testing.T) {
	v := NewVariables()
	done := make(chan bool, 100)

	// Concurrent writes
	for i := 0; i < 50; i++ {
		go func(id int) {
			name := fmt.Sprintf("var%d", id)
			_ = v.SetVariable(name, float64(id))
			done <- true
		}(i)
	}

	// Concurrent reads
	for i := 0; i < 50; i++ {
		go func(id int) {
			name := fmt.Sprintf("var%d", id)
			v.GetVariable(name)
			done <- true
		}(i)
	}

	// Wait for all operations
	for i := 0; i < 100; i++ {
		<-done
	}

	// Verify final count
	if v.Count() != 50 {
		t.Errorf("Final count = %d, want 50", v.Count())
	}
}