Merge pull request #69 from mahadevTW/indexOfMethod

Index of method
pull/62/merge
Emir Pasic 7 years ago committed by GitHub
commit b2394dfbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -98,6 +98,18 @@ func (list *List) Values() []interface{} {
return newElements
}
//IndexOf returns index of provided element
func (list *List) IndexOf(value interface{}) int{
if list.size == 0 {
return -1
}
for index, element := range list.elements {
if element == value {
return index
}
}
return -1
}
// Empty returns true if list does not contain any elements.
func (list *List) Empty() bool {
return list.size == 0

@ -6,8 +6,9 @@ package arraylist
import (
"fmt"
"github.com/emirpasic/gods/utils"
"testing"
"github.com/emirpasic/gods/utils"
)
func TestListAdd(t *testing.T) {
@ -25,6 +26,33 @@ func TestListAdd(t *testing.T) {
}
}
func TestListIndexOf(t *testing.T) {
list := New()
expectedIndex := -1
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
list.Add("a")
list.Add("b", "c")
expectedIndex = 0
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 1
if index := list.IndexOf("b"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 2
if index := list.IndexOf("c"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
}
func TestListRemove(t *testing.T) {
list := New()
list.Add("a")

@ -173,6 +173,18 @@ func (list *List) Values() []interface{} {
return values
}
//IndexOf returns index of provided element
func (list *List) IndexOf(value interface{}) int {
if list.size == 0 {
return -1
}
for index, element := range list.Values() {
if element == value {
return index
}
}
return -1
}
// Empty returns true if list does not contain any elements.
func (list *List) Empty() bool {
return list.size == 0

@ -133,6 +133,33 @@ func TestListValues(t *testing.T) {
}
}
func TestListIndexOf(t *testing.T) {
list := New()
expectedIndex := -1
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
list.Add("a")
list.Add("b", "c")
expectedIndex = 0
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 1
if index := list.IndexOf("b"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 2
if index := list.IndexOf("c"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
}
func TestListInsert(t *testing.T) {
list := New()
list.Insert(0, "b", "c")

@ -154,6 +154,18 @@ func (list *List) Values() []interface{} {
return values
}
//IndexOf returns index of provided element
func (list *List) IndexOf(value interface{}) int{
if list.size == 0 {
return -1
}
for index, element := range list.Values() {
if element == value {
return index
}
}
return -1
}
// Empty returns true if list does not contain any elements.
func (list *List) Empty() bool {
return list.size == 0

@ -133,6 +133,33 @@ func TestListValues(t *testing.T) {
}
}
func TestListIndexOf(t *testing.T) {
list := New()
expectedIndex := -1
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
list.Add("a")
list.Add("b", "c")
expectedIndex = 0
if index := list.IndexOf("a"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 1
if index := list.IndexOf("b"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
expectedIndex = 2
if index := list.IndexOf("c"); index != expectedIndex{
t.Errorf("Got %v expected %v",index,expectedIndex)
}
}
func TestListInsert(t *testing.T) {
list := New()
list.Insert(0, "b", "c")

Loading…
Cancel
Save