1+ package runtime_test
2+
3+ import (
4+ "context"
5+ "testing"
6+
7+ . "github.com/smartystreets/goconvey/convey"
8+
9+ "github.com/MontFerret/ferret/pkg/runtime"
10+ )
11+
12+ func TestArrayExtended (t * testing.T ) {
13+ ctx := context .Background ()
14+
15+ Convey ("Array Extended Functionality" , t , func () {
16+
17+ Convey (".Contains" , func () {
18+ Convey ("Should return true when element exists" , func () {
19+ arr := runtime .NewArrayWith (
20+ runtime .NewInt (1 ),
21+ runtime .NewString ("test" ),
22+ runtime .NewFloat (3.14 ),
23+ )
24+
25+ contains , err := arr .Contains (ctx , runtime .NewInt (1 ))
26+ So (err , ShouldBeNil )
27+ So (contains , ShouldEqual , runtime .True )
28+
29+ contains , err = arr .Contains (ctx , runtime .NewString ("test" ))
30+ So (err , ShouldBeNil )
31+ So (contains , ShouldEqual , runtime .True )
32+
33+ contains , err = arr .Contains (ctx , runtime .NewFloat (3.14 ))
34+ So (err , ShouldBeNil )
35+ So (contains , ShouldEqual , runtime .True )
36+ })
37+
38+ Convey ("Should return false when element does not exist" , func () {
39+ arr := runtime .NewArrayWith (
40+ runtime .NewInt (1 ),
41+ runtime .NewString ("test" ),
42+ )
43+
44+ contains , err := arr .Contains (ctx , runtime .NewInt (2 ))
45+ So (err , ShouldBeNil )
46+ So (contains , ShouldEqual , runtime .False )
47+
48+ contains , err = arr .Contains (ctx , runtime .NewString ("other" ))
49+ So (err , ShouldBeNil )
50+ So (contains , ShouldEqual , runtime .False )
51+ })
52+
53+ Convey ("Should return false for empty array" , func () {
54+ arr := runtime .NewArray (0 )
55+
56+ contains , err := arr .Contains (ctx , runtime .NewInt (1 ))
57+ So (err , ShouldBeNil )
58+ So (contains , ShouldEqual , runtime .False )
59+ })
60+ })
61+
62+ Convey (".IndexOf" , func () {
63+ Convey ("Should return correct index when element exists" , func () {
64+ arr := runtime .NewArrayWith (
65+ runtime .NewInt (1 ),
66+ runtime .NewString ("test" ),
67+ runtime .NewFloat (3.14 ),
68+ )
69+
70+ index , err := arr .IndexOf (ctx , runtime .NewInt (1 ))
71+ So (err , ShouldBeNil )
72+ So (index , ShouldEqual , runtime .NewInt (0 ))
73+
74+ index , err = arr .IndexOf (ctx , runtime .NewString ("test" ))
75+ So (err , ShouldBeNil )
76+ So (index , ShouldEqual , runtime .NewInt (1 ))
77+
78+ index , err = arr .IndexOf (ctx , runtime .NewFloat (3.14 ))
79+ So (err , ShouldBeNil )
80+ So (index , ShouldEqual , runtime .NewInt (2 ))
81+ })
82+
83+ Convey ("Should return -1 when element does not exist" , func () {
84+ arr := runtime .NewArrayWith (
85+ runtime .NewInt (1 ),
86+ runtime .NewString ("test" ),
87+ )
88+
89+ index , err := arr .IndexOf (ctx , runtime .NewInt (2 ))
90+ So (err , ShouldBeNil )
91+ So (index , ShouldEqual , runtime .NewInt (- 1 ))
92+
93+ index , err = arr .IndexOf (ctx , runtime .NewString ("other" ))
94+ So (err , ShouldBeNil )
95+ So (index , ShouldEqual , runtime .NewInt (- 1 ))
96+ })
97+
98+ Convey ("Should return -1 for empty array" , func () {
99+ arr := runtime .NewArray (0 )
100+
101+ index , err := arr .IndexOf (ctx , runtime .NewInt (1 ))
102+ So (err , ShouldBeNil )
103+ So (index , ShouldEqual , runtime .NewInt (- 1 ))
104+ })
105+ })
106+
107+ Convey (".First" , func () {
108+ Convey ("Should return first element" , func () {
109+ arr := runtime .NewArrayWith (
110+ runtime .NewInt (1 ),
111+ runtime .NewString ("test" ),
112+ runtime .NewFloat (3.14 ),
113+ )
114+
115+ first , err := arr .First (ctx )
116+ So (err , ShouldBeNil )
117+ So (first , ShouldEqual , runtime .NewInt (1 ))
118+ })
119+
120+ Convey ("Should return None for empty array" , func () {
121+ arr := runtime .NewArray (0 )
122+
123+ first , err := arr .First (ctx )
124+ So (err , ShouldBeNil )
125+ So (first , ShouldEqual , runtime .None )
126+ })
127+ })
128+
129+ Convey (".Last" , func () {
130+ Convey ("Should return last element" , func () {
131+ arr := runtime .NewArrayWith (
132+ runtime .NewInt (1 ),
133+ runtime .NewString ("test" ),
134+ runtime .NewFloat (3.14 ),
135+ )
136+
137+ last , err := arr .Last (ctx )
138+ So (err , ShouldBeNil )
139+ So (last , ShouldEqual , runtime .NewFloat (3.14 ))
140+ })
141+
142+ Convey ("Should return None for empty array" , func () {
143+ arr := runtime .NewArray (0 )
144+
145+ last , err := arr .Last (ctx )
146+ So (err , ShouldBeNil )
147+ So (last , ShouldEqual , runtime .None )
148+ })
149+ })
150+
151+ Convey (".IsEmpty" , func () {
152+ Convey ("Should return true for empty array" , func () {
153+ arr := runtime .NewArray (0 )
154+
155+ isEmpty , err := arr .IsEmpty (ctx )
156+ So (err , ShouldBeNil )
157+ So (isEmpty , ShouldEqual , runtime .True )
158+ })
159+
160+ Convey ("Should return false for non-empty array" , func () {
161+ arr := runtime .NewArrayWith (runtime .NewInt (1 ))
162+
163+ isEmpty , err := arr .IsEmpty (ctx )
164+ So (err , ShouldBeNil )
165+ So (isEmpty , ShouldEqual , runtime .False )
166+ })
167+ })
168+
169+ Convey (".Clear" , func () {
170+ Convey ("Should clear all elements" , func () {
171+ arr := runtime .NewArrayWith (
172+ runtime .NewInt (1 ),
173+ runtime .NewString ("test" ),
174+ runtime .NewFloat (3.14 ),
175+ )
176+
177+ length , _ := arr .Length (ctx )
178+ So (length , ShouldEqual , runtime .NewInt (3 ))
179+
180+ err := arr .Clear (ctx )
181+ So (err , ShouldBeNil )
182+
183+ length , _ = arr .Length (ctx )
184+ So (length , ShouldEqual , runtime .NewInt (0 ))
185+
186+ isEmpty , _ := arr .IsEmpty (ctx )
187+ So (isEmpty , ShouldEqual , runtime .True )
188+ })
189+
190+ Convey ("Should work on empty array" , func () {
191+ arr := runtime .NewArray (0 )
192+
193+ err := arr .Clear (ctx )
194+ So (err , ShouldBeNil )
195+
196+ length , _ := arr .Length (ctx )
197+ So (length , ShouldEqual , runtime .NewInt (0 ))
198+ })
199+ })
200+
201+ Convey (".Copy" , func () {
202+ Convey ("Should create a shallow copy" , func () {
203+ arr := runtime .NewArrayWith (
204+ runtime .NewInt (1 ),
205+ runtime .NewString ("test" ),
206+ runtime .NewFloat (3.14 ),
207+ )
208+
209+ copied := arr .Copy ()
210+ copyArr := copied .(* runtime.Array )
211+
212+ // Should be equal but different instances
213+ So (arr .Compare (copyArr ), ShouldEqual , 0 )
214+
215+ // Modifying copy should not affect original
216+ copyArr .Add (ctx , runtime .NewInt (42 ))
217+
218+ arrLength , _ := arr .Length (ctx )
219+ copyLength , _ := copyArr .Length (ctx )
220+
221+ So (arrLength , ShouldEqual , runtime .NewInt (3 ))
222+ So (copyLength , ShouldEqual , runtime .NewInt (4 ))
223+ })
224+
225+ Convey ("Should copy empty array" , func () {
226+ arr := runtime .NewArray (0 )
227+
228+ copied := arr .Copy ()
229+ copyArr := copied .(* runtime.Array )
230+
231+ So (arr .Compare (copyArr ), ShouldEqual , 0 )
232+
233+ isEmpty , _ := copyArr .IsEmpty (ctx )
234+ So (isEmpty , ShouldEqual , runtime .True )
235+ })
236+ })
237+
238+ Convey (".Remove" , func () {
239+ Convey ("Should remove existing element" , func () {
240+ arr := runtime .NewArrayWith (
241+ runtime .NewInt (1 ),
242+ runtime .NewString ("test" ),
243+ runtime .NewFloat (3.14 ),
244+ runtime .NewString ("test" ),
245+ )
246+
247+ err := arr .Remove (ctx , runtime .NewString ("test" ))
248+ So (err , ShouldBeNil )
249+
250+ length , _ := arr .Length (ctx )
251+ So (length , ShouldEqual , runtime .NewInt (3 ))
252+
253+ // Should remove first occurrence
254+ val0 , _ := arr .Get (ctx , runtime .NewInt (0 ))
255+ So (val0 , ShouldEqual , runtime .NewInt (1 ))
256+
257+ val1 , _ := arr .Get (ctx , runtime .NewInt (1 ))
258+ So (val1 , ShouldEqual , runtime .NewFloat (3.14 ))
259+
260+ val2 , _ := arr .Get (ctx , runtime .NewInt (2 ))
261+ So (val2 , ShouldEqual , runtime .NewString ("test" ))
262+ })
263+
264+ Convey ("Should return nil when element does not exist" , func () {
265+ arr := runtime .NewArrayWith (
266+ runtime .NewInt (1 ),
267+ runtime .NewString ("test" ),
268+ )
269+
270+ err := arr .Remove (ctx , runtime .NewString ("notfound" ))
271+ So (err , ShouldBeNil ) // Returns nil even when not found
272+
273+ length , _ := arr .Length (ctx )
274+ So (length , ShouldEqual , runtime .NewInt (2 ))
275+ })
276+
277+ Convey ("Should work with empty array" , func () {
278+ arr := runtime .NewArray (0 )
279+
280+ err := arr .Remove (ctx , runtime .NewInt (1 ))
281+ So (err , ShouldBeNil ) // Returns nil even when not found
282+
283+ length , _ := arr .Length (ctx )
284+ So (length , ShouldEqual , runtime .NewInt (0 ))
285+ })
286+ })
287+
288+ Convey (".SortAsc" , func () {
289+ Convey ("Should sort integers in ascending order" , func () {
290+ arr := runtime .NewArrayWith (
291+ runtime .NewInt (3 ),
292+ runtime .NewInt (1 ),
293+ runtime .NewInt (4 ),
294+ runtime .NewInt (2 ),
295+ )
296+
297+ err := arr .SortAsc (ctx )
298+ So (err , ShouldBeNil )
299+
300+ val0 , _ := arr .Get (ctx , runtime .NewInt (0 ))
301+ So (val0 , ShouldEqual , runtime .NewInt (1 ))
302+
303+ val1 , _ := arr .Get (ctx , runtime .NewInt (1 ))
304+ So (val1 , ShouldEqual , runtime .NewInt (2 ))
305+
306+ val2 , _ := arr .Get (ctx , runtime .NewInt (2 ))
307+ So (val2 , ShouldEqual , runtime .NewInt (3 ))
308+
309+ val3 , _ := arr .Get (ctx , runtime .NewInt (3 ))
310+ So (val3 , ShouldEqual , runtime .NewInt (4 ))
311+ })
312+
313+ Convey ("Should work with empty array" , func () {
314+ arr := runtime .NewArray (0 )
315+
316+ err := arr .SortAsc (ctx )
317+ So (err , ShouldBeNil )
318+
319+ length , _ := arr .Length (ctx )
320+ So (length , ShouldEqual , runtime .NewInt (0 ))
321+ })
322+ })
323+ })
324+ }
0 commit comments