Skip to content
This repository has been archived by the owner on Dec 29, 2018. It is now read-only.

Commit

Permalink
New array functions and test suite
Browse files Browse the repository at this point in the history
* array_device_index()
* array_integer_index()
* array_double_index()
* array_string_index()
  • Loading branch information
moty66 committed Oct 18, 2013
1 parent 3660b3a commit 0a025ee
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
86 changes: 83 additions & 3 deletions array.axi
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,102 @@ include 'math'
include 'io'


define_function array_function_deprecated() {
define_function array_function_deprecated(char old[64], char new[64]) {

println("'Warning, the array function ',old,'() is deprecated',
', please use ', new, '()'");

}

/**
* Finds the index for an matching entry in an array.
*
* Finds the index for any matching device entry in an array.
* @deprecated
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_index(dev item, dev list[])
{
array_function_deprecated('array_index', 'array_device_index');
return array_device_index(item, list);
}

/**
* Finds the index for any matching device entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_device_index(dev item, dev list[])
{
stack_var integer i

for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}

return 0
}

/**
* Finds the index for any matching integer entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_integer_index(integer item, integer list[])
{
stack_var integer i

for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}

return 0
}

/**
* Finds the index for any matching double entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_double_index(double item, double list[])
{
stack_var integer i

for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}

return 0
}

/**
* Finds the index for any matching string entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_string_index(char item[], char list[][])
{
stack_var integer i

for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
Expand Down
65 changes: 65 additions & 0 deletions test/test_array.axi
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
program_name='test_array'

#if_not_defined __NCL_LIB_TEST_ARRAY
#define __NCL_LIB_TEST_ARRAY

#include 'io'
#include 'array'

define_device
dev1 = 5001:1:3;
dev2 = 5001:2:3;
dev3 = 5001:3:3;
dev4 = 5001:4:3;
dev5 = 5001:5:3;



define_variable
dev devices[5] = { dev1, dev2, dev3, dev4, dev5}
dev devS = 5001:1:3;
integer integers[5] = {10,20,30,40,50}
double doubles[5] = {10.1,20.2,30.3,40.4,50.5}
char strings[5][5]={'foo','bar','baz','qux','quux'}

/**
* Test functionality of some array functions
*/

define_function test_array()
{
println("':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'")
println("'Running array library test suite.'")
println("' '")
println("'Devices = {5001:1:3, 5001:2:3, 5001:3:3, 5001:4:3, 5001:5:3}'")
println("'Searching index of 5001:1:3'")
println("itoa(array_device_index(devS, devices))")
println("'Searching index of 1001:1:0'")
println("itoa(array_device_index(1001:1:1, devices))")
println("'Searching index of 5001:1:3 using deprecated function'")
println("itoa(array_index(devS, devices))")

println("' '")
println("'integers = {10,20,30,40,50}'")
println("'Searching index of 20'")
println("itoa(array_integer_index(20, integers))")
println("'Searching index of 200'")
println("itoa(array_integer_index(200, integers))")

println("' '")
println("'doubles = {10.1,20.2,30.3,40.4,50.5}'")
println("'Searching index of 30.3'")
println("itoa(array_double_index(30.3, doubles))")
println("'Searching index of 30.33'")
println("itoa(array_double_index(30.33, doubles))")

println("' '")
println("'strings = {''foo'',''bar'',''baz'',''qux'',''quux''}'")
println("'Searching index of qux'")
println("itoa(array_string_index("'qux'", strings))")
println("'Searching index of fo'")
println("itoa(array_string_index("'fo'", strings))")

}

#end_if

0 comments on commit 0a025ee

Please sign in to comment.