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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
// 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())
}
}
// TestVariablesSaveLoad tests saving and loading variables to/from a file
func TestVariablesSaveLoad(t *testing.T) {
tmpFile := t.TempDir() + "/vars.json"
// Create and populate a variable store
vars := NewVariables()
vars.SetVariable("x", 42)
vars.SetVariable("y", 100)
// Save to file
if err := vars.Save(tmpFile); err != nil {
t.Fatalf("Save failed: %v", err)
}
// Create a new variable store and load from file
newVars := NewVariables()
if err := newVars.Load(tmpFile); err != nil {
t.Fatalf("Load failed: %v", err)
}
// Verify loaded values
val, exists := newVars.GetVariable("x")
if !exists || val != 42 {
t.Errorf("Expected x=42 after load, got %v (exists=%v)", val, exists)
}
val, exists = newVars.GetVariable("y")
if !exists || val != 100 {
t.Errorf("Expected y=100 after load, got %v (exists=%v)", val, exists)
}
// Verify that variables from original store are NOT in the new one
val, exists = newVars.GetVariable("z")
if exists {
t.Errorf("Unexpected variable z in loaded store: %v", val)
}
}
// TestVariablesSaveLoadNonExistentFile tests that Load handles non-existent files gracefully
func TestVariablesSaveLoadNonExistentFile(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := tmpDir + "/nonexistent_vars.json"
vars := NewVariables()
// Load from non-existent file should not error
if err := vars.Load(tmpFile); err != nil {
t.Errorf("Load from non-existent file should not error: %v", err)
}
// Verify the store is empty
if vars.Count() != 0 {
t.Errorf("Expected empty store after loading non-existent file, got %d variables", vars.Count())
}
}
// TestVariablesSaveLoadOverwrite tests that Load overwrites existing variables
func TestVariablesSaveLoadOverwrite(t *testing.T) {
tmpFile := t.TempDir() + "/vars.json"
// Create and save variables
vars := NewVariables()
vars.SetVariable("x", 10)
vars.SetVariable("y", 20)
if err := vars.Save(tmpFile); err != nil {
t.Fatalf("Save failed: %v", err)
}
// Modify variables in original store
vars.SetVariable("x", 100)
vars.SetVariable("z", 30)
// Load from file (should overwrite)
if err := vars.Load(tmpFile); err != nil {
t.Fatalf("Load failed: %v", err)
}
// Verify values are from file
val, exists := vars.GetVariable("x")
if !exists || val != 10 {
t.Errorf("Expected x=10 after load, got %v", val)
}
val, exists = vars.GetVariable("y")
if !exists || val != 20 {
t.Errorf("Expected y=20 after load, got %v", val)
}
// z should not exist anymore
val, exists = vars.GetVariable("z")
if exists {
t.Errorf("Expected z to be removed, got %v", val)
}
}
// TestVariablesSaveLoadEmptyStore tests saving and loading an empty variable store
func TestVariablesSaveLoadEmptyStore(t *testing.T) {
tmpFile := t.TempDir() + "/empty_vars.json"
// Create empty variable store
vars := NewVariables()
// Save empty store
if err := vars.Save(tmpFile); err != nil {
t.Fatalf("Save failed: %v", err)
}
// Load into new store
newVars := NewVariables()
if err := newVars.Load(tmpFile); err != nil {
t.Fatalf("Load failed: %v", err)
}
// Verify empty
if newVars.Count() != 0 {
t.Errorf("Expected empty store, got %d variables", newVars.Count())
}
}
|