From 66e68e1372622bd1a87cc76bd47b3db96535c025 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Sat, 24 Mar 2012 01:56:16 +0000 Subject: [PATCH] Added a simple stack implementation based on ArrayList since java.util.ArrayDeque is not available on Froyo. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/branches/froyo_12.03@484 180e2498-e6e9-4542-8430-84ac67f01cd8 --- .../net/sf/openrocket/util/SimpleStack.java | 29 +++++++++++++ .../sf/openrocket/util/SimpleStackTest.java | 43 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 core/src/net/sf/openrocket/util/SimpleStack.java create mode 100644 core/test/net/sf/openrocket/util/SimpleStackTest.java diff --git a/core/src/net/sf/openrocket/util/SimpleStack.java b/core/src/net/sf/openrocket/util/SimpleStack.java new file mode 100644 index 00000000..6bdd9671 --- /dev/null +++ b/core/src/net/sf/openrocket/util/SimpleStack.java @@ -0,0 +1,29 @@ +package net.sf.openrocket.util; + +import java.util.NoSuchElementException; +/** + * SimpleStack implementation backed by an ArrayList. + * + */ +public class SimpleStack extends ArrayList { + + public void push( T value ) { + this.add(value); + } + + public T peek() { + if ( size() <= 0 ) { + return null; + } + return this.get( size() -1 ); + } + + public T pop() { + if ( size() <= 0 ) { + throw new NoSuchElementException(); + } + T value = this.remove( size() -1 ); + return value; + } + +} diff --git a/core/test/net/sf/openrocket/util/SimpleStackTest.java b/core/test/net/sf/openrocket/util/SimpleStackTest.java new file mode 100644 index 00000000..16cd312b --- /dev/null +++ b/core/test/net/sf/openrocket/util/SimpleStackTest.java @@ -0,0 +1,43 @@ +package net.sf.openrocket.util; + +import java.util.NoSuchElementException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class SimpleStackTest { + + @Test(expected=NoSuchElementException.class) + public void testEmptyStack() { + SimpleStack s = new SimpleStack(); + + assertNull(s.peek()); + + s.pop(); + } + + @Test + public void testPushAndPop() { + + SimpleStack s = new SimpleStack(); + + for( int i = 0; i< 10; i++ ) { + s.push(i); + assertEquals(i+1, s.size()); + } + + for( int i=9; i>= 0; i-- ) { + assertEquals( i, s.peek().intValue() ); + Integer val = s.pop(); + assertEquals( i, val.intValue() ); + assertEquals( i, s.size() ); + } + + assertNull( s.peek() ); + assertEquals( 0, s.size() ); + + } + +} -- 2.47.2