- iterator tests

- container_test fix
pull/12/head
Emir Pasic 9 years ago
parent 342ccbef84
commit 6fefe7cc24

@ -34,28 +34,28 @@ import (
) )
// For testing purposes // For testing purposes
type Container struct { type ContainerTest struct {
values []interface{} values []interface{}
} }
func (container Container) Empty() bool { func (container ContainerTest) Empty() bool {
return len(container.values) == 0 return len(container.values) == 0
} }
func (container Container) Size() int { func (container ContainerTest) Size() int {
return len(container.values) return len(container.values)
} }
func (container Container) Clear() { func (container ContainerTest) Clear() {
container.values = []interface{}{} container.values = []interface{}{}
} }
func (container Container) Values() []interface{} { func (container ContainerTest) Values() []interface{} {
return container.values return container.values
} }
func TestGetSortedValuesInts(t *testing.T) { func TestGetSortedValuesInts(t *testing.T) {
container := Container{} container := ContainerTest{}
container.values = []interface{}{5, 1, 3, 2, 4} container.values = []interface{}{5, 1, 3, 2, 4}
values := GetSortedValues(container, utils.IntComparator) values := GetSortedValues(container, utils.IntComparator)
for i := 1; i < container.Size(); i++ { for i := 1; i < container.Size(); i++ {
@ -66,7 +66,7 @@ func TestGetSortedValuesInts(t *testing.T) {
} }
func TestGetSortedValuesStrings(t *testing.T) { func TestGetSortedValuesStrings(t *testing.T) {
container := Container{} container := ContainerTest{}
container.values = []interface{}{"g", "a", "d", "e", "f", "c", "b"} container.values = []interface{}{"g", "a", "d", "e", "f", "c", "b"}
values := GetSortedValues(container, utils.StringComparator) values := GetSortedValues(container, utils.StringComparator)
for i := 1; i < container.Size(); i++ { for i := 1; i < container.Size(); i++ {

@ -55,19 +55,20 @@ type Iterator struct {
} }
func (list *List) Iterator() Iterator { func (list *List) Iterator() Iterator {
return Iterator{list: list, current: 0} return Iterator{list: list, current: -1}
} }
func (iterator *Iterator) Next() bool { func (iterator *Iterator) Next() bool {
return false iterator.current += 1
return iterator.list.withinRange(iterator.current)
} }
func (iterator *Iterator) Value() interface{} { func (iterator *Iterator) Value() interface{} {
return nil return iterator.list.elements[iterator.current]
} }
func (iterator *Iterator) Index() interface{} { func (iterator *Iterator) Index() interface{} {
return nil return iterator.list.elements[iterator.current]
} }
const ( const (
@ -108,7 +109,7 @@ func (list *List) Remove(index int) {
} }
list.elements[index] = nil // cleanup reference list.elements[index] = nil // cleanup reference
copy(list.elements[index:], list.elements[index+1:list.size]) // shift to the left by one (slow operation, need ways to optimize this) copy(list.elements[index:], list.elements[index + 1:list.size]) // shift to the left by one (slow operation, need ways to optimize this)
list.size -= 1 list.size -= 1
list.shrink() list.shrink()
@ -190,12 +191,12 @@ func (list *List) Insert(index int, values ...interface{}) {
list.growBy(l) list.growBy(l)
list.size += l list.size += l
// Shift old to right // Shift old to right
for i := list.size - 1; i >= index+l; i-- { for i := list.size - 1; i >= index + l; i-- {
list.elements[i] = list.elements[i-l] list.elements[i] = list.elements[i - l]
} }
// Insert new // Insert new
for i, value := range values { for i, value := range values {
list.elements[index+i] = value list.elements[index + i] = value
} }
} }
@ -275,8 +276,8 @@ func (list *List) resize(cap int) {
func (list *List) growBy(n int) { func (list *List) growBy(n int) {
// When capacity is reached, grow by a factor of GROWTH_FACTOR and add number of elements // When capacity is reached, grow by a factor of GROWTH_FACTOR and add number of elements
currentCapacity := cap(list.elements) currentCapacity := cap(list.elements)
if list.size+n >= currentCapacity { if list.size + n >= currentCapacity {
newCapacity := int(GROWTH_FACTOR * float32(currentCapacity+n)) newCapacity := int(GROWTH_FACTOR * float32(currentCapacity + n))
list.resize(newCapacity) list.resize(newCapacity)
} }
} }
@ -288,7 +289,7 @@ func (list *List) shrink() {
} }
// Shrink when size is at SHRINK_FACTOR * capacity // Shrink when size is at SHRINK_FACTOR * capacity
currentCapacity := cap(list.elements) currentCapacity := cap(list.elements)
if list.size <= int(float32(currentCapacity)*SHRINK_FACTOR) { if list.size <= int(float32(currentCapacity) * SHRINK_FACTOR) {
list.resize(list.size) list.resize(list.size)
} }
} }

Loading…
Cancel
Save