Permalink
Browse files

Finished testing integer ops.

  • Loading branch information...
1 parent 54aa591 commit 6ccbed575f2050a57c3872f9129d63feaae2dd31 Jeremiah Martell committed Oct 8, 2011
@@ -35,12 +35,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
typedef enum
{
TROT_LIST_SUCCESS = 0,
- TROT_LIST_ERROR_GENERAL = -1,
- TROT_LIST_ERROR_PRECOND = -2,
- TROT_LIST_ERROR_PARANOID = -3,
- TROT_LIST_ERROR_MEMORY_ALLOCATION_FAILED = -4,
- TROT_LIST_ERROR_BAD_INDEX = -5,
- TROT_LIST_ERROR_WRONG_KIND = -6,
+ TROT_LIST_ERROR_PRECOND = -1,
+ TROT_LIST_ERROR_PARANOID = -2,
+ TROT_LIST_ERROR_MEMORY_ALLOCATION_FAILED = -3,
+ TROT_LIST_ERROR_BAD_INDEX = -4,
+ TROT_LIST_ERROR_WRONG_KIND = -5,
+ TROT_LIST_ERROR_INVALID_OP = -6,
TROT_LIST_ERROR_DIVIDE_BY_ZERO = -7
} TROT_RC;
@@ -60,8 +60,6 @@ TROT_RC trotListIntOperand( trotListRef *lr, TROT_INT_OPERAND op )
/* PRECOND */
PRECOND_ERR_IF( lr == NULL );
- PRECOND_ERR_IF( op < TROT_INT_OPERAND_MIN );
- PRECOND_ERR_IF( op > TROT_INT_OPERAND_MAX );
/* CODE */
@@ -72,38 +70,45 @@ TROT_RC trotListIntOperand( trotListRef *lr, TROT_INT_OPERAND op )
/* check that last value in list is an int */
ERR_IF( node -> kind != NODE_KIND_INT, TROT_LIST_ERROR_WRONG_KIND );
- /* if not NEG or NOT, we need to remove an int */
- if ( op != TROT_INT_OPERAND_NEG && op != TROT_INT_OPERAND_LOGICAL_NOT )
+ /* handle single value ops */
+ if ( op == TROT_INT_OPERAND_NEG )
{
- /* check that second-to-last value in list is an int */
- /* We technically don't have to do this here, but since we're
- going to remove an int, I would rather leave the list
- untouched on error. So let's catch the error here. */
- if ( node -> count < 1 )
- {
- ERR_IF( node -> prev -> kind != NODE_KIND_INT, TROT_LIST_ERROR_WRONG_KIND );
- }
-
- /* remove last int */
- value = node -> n[ (node -> count) - 1 ];
- node -> count -= 1;
- l -> childrenCount -= 1;
-
- if ( node -> count == 0 )
- {
- node -> prev -> next = node -> next;
- node -> next -> prev = node -> prev;
-
- trotFree( node -> n );
- trotFree( node );
- }
+ node -> n[ (node -> count) - 1 ] = (-(node -> n[ (node -> count) - 1 ] ) );
+ return TROT_LIST_SUCCESS;
+ }
+
+ if ( op == TROT_INT_OPERAND_LOGICAL_NOT )
+ {
+ node -> n[ (node -> count) - 1 ] = ! ( node -> n[ (node -> count) - 1 ] );
+ return TROT_LIST_SUCCESS;
+ }
+
+ /* check that second-to-last value in list is an int */
+ /* We technically don't have to do this here, but since we're
+ going to remove an int, I would rather leave the list
+ untouched on error. So let's catch the error here. */
+ if ( node -> count <= 1 )
+ {
+ ERR_IF( node -> prev -> kind != NODE_KIND_INT, TROT_LIST_ERROR_WRONG_KIND );
+ }
+
+ /* remove last int */
+ value = node -> n[ (node -> count) - 1 ];
+ node -> count -= 1;
+ l -> childrenCount -= 1;
+
+ if ( node -> count == 0 )
+ {
+ node -> prev -> next = node -> next;
+ node -> next -> prev = node -> prev;
+
+ trotFree( node -> n );
+ trotFree( node );
}
/* now we can call trotListIntOperandValue */
rc = trotListIntOperandValue( lr, op, value );
- ERR_IF_PASSTHROUGH;
-
- return TROT_LIST_SUCCESS;
+ return rc;
/* CLEANUP */
@@ -132,8 +137,6 @@ TROT_RC trotListIntOperandValue( trotListRef *lr, TROT_INT_OPERAND op, INT_TYPE
/* PRECOND */
PRECOND_ERR_IF( lr == NULL );
- PRECOND_ERR_IF( op < TROT_INT_OPERAND_MIN );
- PRECOND_ERR_IF( op > TROT_INT_OPERAND_MAX );
/* CODE */
@@ -162,21 +165,15 @@ TROT_RC trotListIntOperandValue( trotListRef *lr, TROT_INT_OPERAND op, INT_TYPE
ERR_IF( value == 0, TROT_LIST_ERROR_DIVIDE_BY_ZERO );
node -> n[ (node -> count) - 1 ] %= value;
break;
- case TROT_INT_OPERAND_NEG:
- /* NOTE: value not used */
- node -> n[ (node -> count) - 1 ] = (-(node -> n[ (node -> count) - 1 ] ) );
- break;
case TROT_INT_OPERAND_LOGICAL_AND:
node -> n[ (node -> count) - 1 ] = node -> n[ (node -> count) - 1 ] && value;
break;
case TROT_INT_OPERAND_LOGICAL_OR:
node -> n[ (node -> count) - 1 ] = node -> n[ (node -> count) - 1 ] || value;
break;
- case TROT_INT_OPERAND_LOGICAL_NOT:
- /* NOTE: value not used */
- node -> n[ (node -> count) - 1 ] = ! ( node -> n[ (node -> count) - 1 ] );
- break;
+ default:
+ ERR_IF( 1, TROT_LIST_ERROR_INVALID_OP );
}
return TROT_LIST_SUCCESS;
@@ -0,0 +1,106 @@
+/*
+Copyright (c) 2010,2011, Jeremiah Martell
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ - Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ - Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ - Neither the name of Jeremiah Martell nor the name of GeekHorse nor the
+ name of Trot nor the names of its contributors may be used to endorse or
+ promote products derived from this software without specific prior written
+ permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/******************************************************************************/
+#include "trotCommon.h"
+#include "trotList.h"
+#include "trotListInternal.h"
+
+#include "testCommon.h"
+
+
+/******************************************************************************/
+int testBadIntegerOps()
+{
+ /* DATA */
+ int rc = 0;
+
+ trotListRef *lr = NULL;
+
+
+ /* CODE */
+ /* **************************************** */
+ /* test wrong kinds */
+ printf( "Testing bad integer ops...\n" ); fflush( stdout );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListRefAppendInt( lr, 1 ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( trotListRefAppendInt( lr, 1 ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListRefAppendInt( lr, 1 ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( trotListIntOperand( lr, TROT_INT_OPERAND_ADD ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListIntOperandValue( lr, TROT_INT_OPERAND_ADD, 1 ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( addListWithValue( lr, -1, 1 ) != 0 );
+ TEST_ERR_IF( trotListIntOperandValue( lr, TROT_INT_OPERAND_ADD, 1 ) != TROT_LIST_ERROR_WRONG_KIND );
+ trotListRefFree( &lr );
+
+ TEST_ERR_IF( trotListRefInit( &lr ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListRefAppendInt( lr, 1 ) != TROT_LIST_SUCCESS );
+ TEST_ERR_IF( trotListIntOperandValue( lr, -99, 1 ) != TROT_LIST_ERROR_INVALID_OP );
+ trotListRefFree( &lr );
+
+ return 0;
+
+
+ /* CLEANUP */
+ cleanup:
+
+ return rc;
+}
+
@@ -57,6 +57,7 @@ int testBadTypesAndIndices();
int testPrimaryFunctionality();
int testSecondaryFunctionality();
int testIntOperands();
+int testBadIntegerOps();
/******************************************************************************/
/* create functions */
Oops, something went wrong.

0 comments on commit 6ccbed5

Please sign in to comment.