MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / TestDynamicArray

Function TestDynamicArray

structure/dynamicarray/dynamicarray_test.go:8–97  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

6)
7
8func TestDynamicArray(t *testing.T) {
9 numbers := DynamicArray{}
10
11 // check numbers is empty or nut
12 t.Run("Check Empty Dynamic Array", func(t *testing.T) {
13 if numbers.IsEmpty() != true {
14 t.Errorf("Expected be true but got %v", numbers.IsEmpty())
15 }
16 })
17
18 numbers.Add(10)
19 numbers.Add(20)
20 numbers.Add(30)
21 numbers.Add(40)
22 numbers.Add(50)
23
24 // check numbers added to our dynamic array
25 t.Run("Add Element into Dynamic Array", func(t *testing.T) {
26 if numbers.IsEmpty() != false {
27 t.Errorf("Expected be false but got %v", numbers.IsEmpty())
28 }
29 var res []any
30 res = append(res, 10)
31 res = append(res, 20)
32 res = append(res, 30)
33 res = append(res, 40)
34 res = append(res, 50)
35
36 if !reflect.DeepEqual(numbers.GetData(), res) {
37 t.Errorf("Expected be [10, 20, 30, 40, 50] but got %v", numbers.GetData())
38 }
39 })
40
41 // Remove an Element inside the dynamic array with the index of array
42 t.Run("Remove in Dynamic Array", func(t *testing.T) {
43 if numbers.IsEmpty() != false {
44 t.Errorf("Expected be false but got %v", numbers.IsEmpty())
45 }
46 var res []any
47 res = append(res, 10)
48 res = append(res, 30)
49 res = append(res, 40)
50 res = append(res, 50)
51
52 // remove the element by the index
53 err := numbers.Remove(1)
54
55 if err != nil {
56 t.Errorf("Expected be [10, 30, 40, 50] but got an Error %v", err)
57 }
58
59 if !reflect.DeepEqual(numbers.GetData(), res) {
60 t.Errorf("Expected be [10, 30, 40, 50] but got %v", numbers.GetData())
61 }
62 })
63
64 // get one element by the index of the dynamic array
65 t.Run("Get in Dynamic Array", func(t *testing.T) {

Callers

nothing calls this directly

Calls 6

IsEmptyMethod · 0.95
AddMethod · 0.95
GetDataMethod · 0.95
RemoveMethod · 0.95
GetMethod · 0.95
PutMethod · 0.95

Tested by

no test coverage detected