Skip to content

Commit 089708a

Browse files
authored
Cover dynamic array with tests (#318)
2 parents a82de67 + 07d700d commit 089708a

File tree

4 files changed

+128
-36
lines changed

4 files changed

+128
-36
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Common prefixes:
147147

148148
### Pull Requests
149149

150-
- Checkout our [pull request template](https://github.com/TheAlgorithms/Go/blob/master/.github/PULL_REQUEST_TEMPLATE/pull_request.go
150+
- Checkout our [pull request template](https://github.com/TheAlgorithms/Go/blob/master/.github/PULL_REQUEST_TEMPLATE/pull_request.md
151151
)
152152

153153
#### Building Locally

DIRECTORY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
* [Binary Tree](https://github.com/TheAlgorithms/Go/blob/master/data_structures/binary_tree/binary_tree.go)
3434
* [Btree](https://github.com/TheAlgorithms/Go/blob/master/data_structures/binary_tree/btree.go)
3535
* [Node](https://github.com/TheAlgorithms/Go/blob/master/data_structures/binary_tree/node.go)
36-
* Dynamic-Array
37-
* [Dynamic Array](https://github.com/TheAlgorithms/Go/blob/master/data_structures/dynamic-array/dynamic_array.go)
36+
* Dynamic Array
37+
* [Dynamic Array](https://github.com/TheAlgorithms/Go/blob/master/data_structures/dynamic_array/dynamic_array.go)
38+
* [Dynamic Array Test](https://github.com/TheAlgorithms/Go/blob/master/data_structures/dynamic_array/dynamic_array_test.go)
3839
* Hashmap
3940
* [Hashmap](https://github.com/TheAlgorithms/Go/blob/master/data_structures/hashmap/hashmap.go)
4041
* [Hashmap Test](https://github.com/TheAlgorithms/Go/blob/master/data_structures/hashmap/hashmap_test.go)
Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
// Dynamic Array
2+
// description: A dynamic array is quite similar to a regular array, but its size is modifiable during program runtime.
3+
// details: for more details check out those links below here:
4+
// geeks for geeks article : https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
5+
// We can mention that Dynamic Array is like Slice for more detail about `Go` Slices check those articles :
6+
// https://blog.golang.org/slices-intro
7+
// https://blog.golang.org/slices
8+
// authors [Wesllhey Holanda](https://github.com/wesllhey), [Milad](https://github.com/miraddo)
9+
// see dynamic_array.go, dynamic_array_test.go
10+
111
package dynamicarray
212

13+
14+
// errors: used to handle CheckRangeFromIndex function with a reasonable error value
315
import (
416
"errors"
517
)
618

719
var defaultCapacity = 10
820

921
// DynamicArray structure
22+
// size: length of array
23+
// capacity: the maximum length of the segment
24+
// elementData: an array of any type of data with interface
1025
type DynamicArray struct {
1126
size int
1227
capacity int
1328
elementData []interface{}
1429
}
1530

16-
// Put function
31+
// Put function is change/update the value in array with the index and new value
1732
func (da *DynamicArray) Put(index int, element interface{}) error {
1833
err := da.CheckRangeFromIndex(index)
1934

@@ -26,7 +41,7 @@ func (da *DynamicArray) Put(index int, element interface{}) error {
2641
return nil
2742
}
2843

29-
// Add function
44+
// Add function is add new element to our array
3045
func (da *DynamicArray) Add(element interface{}) {
3146
if da.size == da.capacity {
3247
da.NewCapacity()
@@ -36,7 +51,7 @@ func (da *DynamicArray) Add(element interface{}) {
3651
da.size++
3752
}
3853

39-
// Remove function
54+
// Remove function is remove an element with the index
4055
func (da *DynamicArray) Remove(index int) error {
4156
err := da.CheckRangeFromIndex(index)
4257

@@ -52,7 +67,7 @@ func (da *DynamicArray) Remove(index int) error {
5267
return nil
5368
}
5469

55-
// Get function
70+
// Get function is return one element with the index of array
5671
func (da *DynamicArray) Get(index int) (interface{}, error) {
5772
err := da.CheckRangeFromIndex(index)
5873

@@ -63,25 +78,25 @@ func (da *DynamicArray) Get(index int) (interface{}, error) {
6378
return da.elementData[index], nil
6479
}
6580

66-
// IsEmpty function
81+
// IsEmpty function is check that the array has value or not
6782
func (da *DynamicArray) IsEmpty() bool {
6883
return da.size == 0
6984
}
7085

71-
// GetData function
86+
// GetData function return all value of array
7287
func (da *DynamicArray) GetData() []interface{} {
7388
return da.elementData[:da.size]
7489
}
7590

76-
// CheckRangeFromIndex function
91+
// CheckRangeFromIndex function it will check the range from the index
7792
func (da *DynamicArray) CheckRangeFromIndex(index int) error {
7893
if index >= da.size || index < 0 {
7994
return errors.New("index out of range")
8095
}
8196
return nil
8297
}
8398

84-
// NewCapacity function
99+
// NewCapacity function increase the capacity
85100
func (da *DynamicArray) NewCapacity() {
86101
if da.capacity == 0 {
87102
da.capacity = defaultCapacity
@@ -95,28 +110,3 @@ func (da *DynamicArray) NewCapacity() {
95110

96111
da.elementData = newDataElement
97112
}
98-
99-
// func main() {
100-
// numbers := dynamicArray{}
101-
// fmt.Println(numbers.isEmpty())
102-
103-
// numbers.add(10)
104-
// numbers.add(20)
105-
// numbers.add(30)
106-
// numbers.add(40)
107-
// numbers.add(50)
108-
109-
// fmt.Println(numbers.isEmpty())
110-
111-
// fmt.Println(numbers.getData())
112-
113-
// numbers.remove(1)
114-
115-
// fmt.Println(numbers.getData())
116-
117-
// numberFound, _ := numbers.get(1)
118-
// fmt.Println(numberFound)
119-
120-
// numbers.put(0, 100)
121-
// fmt.Println(numbers.getData())
122-
// }
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package dynamicarray
2+
3+
4+
// reflect: used for check equal values
5+
import (
6+
"reflect"
7+
"testing"
8+
)
9+
10+
// TestDynamicArray some test for dynamic array to make sure everything is ok, and we got the right result
11+
func TestDynamicArray(t *testing.T) {
12+
numbers := DynamicArray{}
13+
14+
// check numbers is empty or nut
15+
t.Run("Check Empty Dynamic Array", func(t *testing.T) {
16+
if numbers.IsEmpty() != true{
17+
t.Errorf("Expected be true but got %v", numbers.IsEmpty())
18+
}
19+
})
20+
21+
numbers.Add(10)
22+
numbers.Add(20)
23+
numbers.Add(30)
24+
numbers.Add(40)
25+
numbers.Add(50)
26+
27+
// check numbers added to our dynamic array
28+
t.Run("Add Element into Dynamic Array", func(t *testing.T) {
29+
if numbers.IsEmpty() != false{
30+
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
31+
}
32+
var res []interface{}
33+
res = append(res, 10)
34+
res = append(res, 20)
35+
res = append(res, 30)
36+
res = append(res, 40)
37+
res = append(res, 50)
38+
39+
if !reflect.DeepEqual(numbers.GetData(), res) {
40+
t.Errorf("Expected be [10, 20, 30, 40, 50] but got %v", numbers.GetData())
41+
}
42+
})
43+
44+
// Remove an Element inside the dynamic array with the index of array
45+
t.Run("Remove in Dynamic Array", func(t *testing.T) {
46+
if numbers.IsEmpty() != false{
47+
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
48+
}
49+
var res []interface{}
50+
res = append(res, 10)
51+
res = append(res, 30)
52+
res = append(res, 40)
53+
res = append(res, 50)
54+
55+
// remove the element by the index
56+
err := numbers.Remove(1)
57+
58+
if err != nil{
59+
t.Errorf("Expected be [10, 30, 40, 50] but got an Error %v", err)
60+
}
61+
62+
if !reflect.DeepEqual(numbers.GetData(), res) {
63+
t.Errorf("Expected be [10, 30, 40, 50] but got %v", numbers.GetData())
64+
}
65+
})
66+
67+
// get one element by the index of the dynamic array
68+
t.Run("Get in Dynamic Array", func(t *testing.T) {
69+
if numbers.IsEmpty() != false{
70+
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
71+
}
72+
73+
// return one element with the index
74+
getOne,_ := numbers.Get(2)
75+
76+
if getOne != 40 {
77+
t.Errorf("Expected be 40 but got %v", getOne)
78+
}
79+
80+
})
81+
82+
83+
// Put to add a value to specific index of Dynamic Array
84+
t.Run("Put to Dynamic Array", func(t *testing.T) {
85+
if numbers.IsEmpty() != false{
86+
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
87+
}
88+
89+
// change value of specific index
90+
err := numbers.Put(0, 100)
91+
if err != nil{
92+
t.Errorf("Expected be [10, 30, 40, 50] but got an Error %v", err)
93+
}
94+
95+
getOne,_ := numbers.Get(0)
96+
if getOne != 100 {
97+
t.Errorf("Expected be 100 but got %v", getOne)
98+
}
99+
})
100+
101+
}

0 commit comments

Comments
 (0)