excel_to_code 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/src/compile/c/a.out CHANGED
Binary file
@@ -5,16 +5,18 @@
5
5
  #include <ctype.h>
6
6
  #include <math.h>
7
7
 
8
- // To run the tests at the end of this file
9
- // cc excel_to_c_runtime; ./a.out
8
+ #ifndef NUMBER_OF_REFS
9
+ #define NUMBER_OF_REFS 0
10
+ #endif
10
11
 
11
- // FIXME: Extract a header file
12
+ #ifndef EXCEL_FILENAME
13
+ #define EXCEL_FILENAME "NoExcelFilename"
14
+ #endif
12
15
 
13
- // I predefine an array of ExcelValues to store calculations
14
- // Probably bad practice. At the very least, I should make it
15
- // link to the cell reference in some way.
16
- #define MAX_EXCEL_VALUE_HEAP_SIZE 1000000
17
- #define MAX_MEMORY_TO_BE_FREED_HEAP_SIZE 1000000
16
+ // Need to retain malloc'd values for a while, so can return to functions that use this library
17
+ // So to avoid a memory leak we keep an array of all the values we have malloc'd, which we then
18
+ // free when the reset() function is called.
19
+ #define MEMORY_TO_BE_FREED_LATER_HEAP_INCREMENT 1000
18
20
 
19
21
  #define true 1
20
22
  #define false 0
@@ -92,26 +94,27 @@ static ExcelValue text(ExcelValue number_v, ExcelValue format_v);
92
94
  static ExcelValue vlookup_3(ExcelValue lookup_value_v,ExcelValue lookup_table_v, ExcelValue column_number_v);
93
95
  static ExcelValue vlookup(ExcelValue lookup_value_v,ExcelValue lookup_table_v, ExcelValue column_number_v, ExcelValue match_type_v);
94
96
 
95
-
96
- // My little heap for excel values
97
- ExcelValue cells[MAX_EXCEL_VALUE_HEAP_SIZE];
98
- int cell_counter = 0;
99
-
100
- #define HEAPCHECK if(cell_counter >= MAX_EXCEL_VALUE_HEAP_SIZE) { printf("ExcelValue heap full. Edit MAX_EXCEL_VALUE_HEAP_SIZE in the c source code."); exit(-1); }
101
-
102
97
  // My little heap for keeping pointers to memory that I need to reclaim
103
- void *memory_that_needs_to_be_freed[MAX_MEMORY_TO_BE_FREED_HEAP_SIZE];
98
+ void **memory_that_needs_to_be_freed;
104
99
  int memory_that_needs_to_be_freed_counter = 0;
105
-
106
- #define MEMORY_THAT_NEEDS_TO_BE_FREED_HEAP_CHECK
100
+ int memory_that_needs_to_be_freed_size = -1;
107
101
 
108
102
  static void free_later(void *pointer) {
103
+ if(memory_that_needs_to_be_freed_counter >= memory_that_needs_to_be_freed_size) {
104
+ if(memory_that_needs_to_be_freed_size <= 0) {
105
+ memory_that_needs_to_be_freed = malloc(MEMORY_TO_BE_FREED_LATER_HEAP_INCREMENT*sizeof(void*));
106
+ memory_that_needs_to_be_freed_size = MEMORY_TO_BE_FREED_LATER_HEAP_INCREMENT;
107
+ } else {
108
+ memory_that_needs_to_be_freed_size += MEMORY_TO_BE_FREED_LATER_HEAP_INCREMENT;
109
+ memory_that_needs_to_be_freed = realloc(memory_that_needs_to_be_freed, memory_that_needs_to_be_freed_size * sizeof(void*));
110
+ if(!memory_that_needs_to_be_freed) {
111
+ printf("Could not allocate new memory to memory that needs to be freed array. halting.");
112
+ exit(-1);
113
+ }
114
+ }
115
+ }
109
116
  memory_that_needs_to_be_freed[memory_that_needs_to_be_freed_counter] = pointer;
110
117
  memory_that_needs_to_be_freed_counter++;
111
- if(memory_that_needs_to_be_freed_counter >= MAX_MEMORY_TO_BE_FREED_HEAP_SIZE) {
112
- printf("Memory that needs to be freed heap full. Edit MAX_MEMORY_TO_BE_FREED_HEAP_SIZE in the c source code");
113
- exit(-1);
114
- }
115
118
  }
116
119
 
117
120
  static void free_all_allocated_memory() {
@@ -122,35 +125,19 @@ static void free_all_allocated_memory() {
122
125
  memory_that_needs_to_be_freed_counter = 0;
123
126
  }
124
127
 
125
- // The object initializers
126
- static ExcelValue new_excel_number(double number) {
127
- cell_counter++;
128
- HEAPCHECK
129
- ExcelValue new_cell = cells[cell_counter];
130
- new_cell.type = ExcelNumber;
131
- new_cell.number = number;
132
- return new_cell;
133
- };
128
+ static int variable_set[NUMBER_OF_REFS];
134
129
 
135
- static ExcelValue new_excel_string(char *string) {
136
- cell_counter++;
137
- HEAPCHECK
138
- ExcelValue new_cell = cells[cell_counter];
139
- new_cell.type = ExcelString;
140
- new_cell.string = string;
141
- return new_cell;
142
- };
130
+ // Resets all cached and malloc'd values
131
+ void reset() {
132
+ free_all_allocated_memory();
133
+ memset(variable_set, 0, sizeof(variable_set));
134
+ }
143
135
 
144
- static ExcelValue new_excel_range(void *array, int rows, int columns) {
145
- cell_counter++;
146
- HEAPCHECK
147
- ExcelValue new_cell = cells[cell_counter];
148
- new_cell.type = ExcelRange;
149
- new_cell.array = array;
150
- new_cell.rows = rows;
151
- new_cell.columns = columns;
152
- return new_cell;
153
- };
136
+ // Handy macros
137
+
138
+ #define new_excel_number(numberdouble) ((ExcelValue) {.type = ExcelNumber, .number = numberdouble})
139
+ #define new_excel_string(stringchar) ((ExcelValue) {.type = ExcelString, .string = stringchar})
140
+ #define new_excel_range(arrayofvalues, rangerows, rangecolumns) ((ExcelValue) {.type = ExcelRange, .array = arrayofvalues, .rows = rangerows, .columns = rangecolumns})
154
141
 
155
142
  static void * new_excel_value_array(int size) {
156
143
  ExcelValue *pointer = malloc(sizeof(ExcelValue)*size); // Freed later
@@ -163,6 +150,8 @@ static void * new_excel_value_array(int size) {
163
150
  };
164
151
 
165
152
  // Constants
153
+ static ExcelValue ORIGINAL_EXCEL_FILENAME = {.type = ExcelString, .string = EXCEL_FILENAME };
154
+
166
155
  const ExcelValue BLANK = {.type = ExcelEmpty, .number = 0};
167
156
 
168
157
  const ExcelValue ZERO = {.type = ExcelNumber, .number = 0};
@@ -292,7 +281,7 @@ static ExcelValue excel_abs(ExcelValue a_v) {
292
281
  if(a >= 0.0 ) {
293
282
  return a_v;
294
283
  } else {
295
- return new_excel_number(-a);
284
+ return (ExcelValue) {.type = ExcelNumber, .number = -a};
296
285
  }
297
286
  }
298
287
 
@@ -1864,759 +1853,3 @@ static ExcelValue hlookup(ExcelValue lookup_value_v,ExcelValue lookup_table_v, E
1864
1853
  }
1865
1854
  return NA;
1866
1855
  }
1867
-
1868
-
1869
- int test_functions() {
1870
- // Test ABS
1871
- assert(excel_abs(ONE).number == 1);
1872
- assert(excel_abs(new_excel_number(-1)).number == 1);
1873
- assert(excel_abs(VALUE).type == ExcelError);
1874
-
1875
- // Test ADD
1876
- assert(add(ONE,new_excel_number(-2.5)).number == -1.5);
1877
- assert(add(ONE,VALUE).type == ExcelError);
1878
-
1879
- // Test AND
1880
- ExcelValue true_array1[] = { TRUE, new_excel_number(10)};
1881
- ExcelValue true_array2[] = { ONE };
1882
- ExcelValue false_array1[] = { FALSE, new_excel_number(10)};
1883
- ExcelValue false_array2[] = { TRUE, new_excel_number(0)};
1884
- // ExcelValue error_array1[] = { new_excel_number(10)}; // Not implemented
1885
- ExcelValue error_array2[] = { TRUE, NA};
1886
- assert(excel_and(2,true_array1).number == 1);
1887
- assert(excel_and(1,true_array2).number == 1);
1888
- assert(excel_and(2,false_array1).number == 0);
1889
- assert(excel_and(2,false_array2).number == 0);
1890
- // assert(excel_and(1,error_array1).type == ExcelError); // Not implemented
1891
- assert(excel_and(2,error_array2).type == ExcelError);
1892
-
1893
- // Test AVERAGE
1894
- ExcelValue array1[] = { new_excel_number(10), new_excel_number(5), TRUE, FALSE};
1895
- ExcelValue array1_v = new_excel_range(array1,2,2);
1896
- ExcelValue array2[] = { array1_v, new_excel_number(9), new_excel_string("Hello")};
1897
- ExcelValue array3[] = { array1_v, new_excel_number(9), new_excel_string("Hello"), VALUE};
1898
- assert(average(4, array1).number == 7.5);
1899
- assert(average(3, array2).number == 8);
1900
- assert(average(4, array3).type == ExcelError);
1901
-
1902
- // Test CHOOSE
1903
- assert(choose(ONE,4,array1).number == 10);
1904
- assert(choose(new_excel_number(4),4,array1).type == ExcelBoolean);
1905
- assert(choose(new_excel_number(0),4,array1).type == ExcelError);
1906
- assert(choose(new_excel_number(5),4,array1).type == ExcelError);
1907
- assert(choose(ONE,4,array3).type == ExcelError);
1908
-
1909
- // Test COUNT
1910
- assert(count(4,array1).number == 2);
1911
- assert(count(3,array2).number == 3);
1912
- assert(count(4,array3).number == 3);
1913
-
1914
- // Test Large
1915
- ExcelValue large_test_array_1[] = { new_excel_number(10), new_excel_number(100), new_excel_number(500), BLANK };
1916
- ExcelValue large_test_array_1_v = new_excel_range(large_test_array_1, 1, 4);
1917
- assert(large(large_test_array_1_v, new_excel_number(1)).number == 500);
1918
- assert(large(large_test_array_1_v, new_excel_number(2)).number == 100);
1919
- assert(large(large_test_array_1_v, new_excel_number(3)).number == 10);
1920
- assert(large(large_test_array_1_v, new_excel_number(4)).type == ExcelError);
1921
- assert(large(new_excel_number(500), new_excel_number(1)).number == 500);
1922
- ExcelValue large_test_array_2[] = { new_excel_number(10), new_excel_number(100), new_excel_number(500), VALUE };
1923
- ExcelValue large_test_array_2_v = new_excel_range(large_test_array_2, 1, 4);
1924
- assert(large(large_test_array_2_v,new_excel_number(2)).type == ExcelError);
1925
- assert(large(new_excel_number(500),VALUE).type == ExcelError);
1926
-
1927
-
1928
- // Test COUNTA
1929
- ExcelValue count_a_test_array_1[] = { new_excel_number(10), new_excel_number(5), TRUE, FALSE, new_excel_string("Hello"), VALUE, BLANK};
1930
- ExcelValue count_a_test_array_1_v = new_excel_range(count_a_test_array_1,7,1);
1931
- ExcelValue count_a_test_array_2[] = {new_excel_string("Bye"),count_a_test_array_1_v};
1932
- assert(counta(7, count_a_test_array_1).number == 6);
1933
- assert(counta(2, count_a_test_array_2).number == 7);
1934
-
1935
- // Test divide
1936
- assert(divide(new_excel_number(12.4),new_excel_number(3.2)).number == 3.875);
1937
- assert(divide(new_excel_number(12.4),new_excel_number(0)).type == ExcelError);
1938
-
1939
- // Test excel_equal
1940
- assert(excel_equal(new_excel_number(1.2),new_excel_number(3.4)).type == ExcelBoolean);
1941
- assert(excel_equal(new_excel_number(1.2),new_excel_number(3.4)).number == false);
1942
- assert(excel_equal(new_excel_number(1.2),new_excel_number(1.2)).number == true);
1943
- assert(excel_equal(new_excel_string("hello"), new_excel_string("HELLO")).number == true);
1944
- assert(excel_equal(new_excel_string("hello world"), new_excel_string("HELLO")).number == false);
1945
- assert(excel_equal(new_excel_string("1"), ONE).number == false);
1946
- assert(excel_equal(DIV0, ONE).type == ExcelError);
1947
-
1948
- // Test not_equal
1949
- assert(not_equal(new_excel_number(1.2),new_excel_number(3.4)).type == ExcelBoolean);
1950
- assert(not_equal(new_excel_number(1.2),new_excel_number(3.4)).number == true);
1951
- assert(not_equal(new_excel_number(1.2),new_excel_number(1.2)).number == false);
1952
- assert(not_equal(new_excel_string("hello"), new_excel_string("HELLO")).number == false);
1953
- assert(not_equal(new_excel_string("hello world"), new_excel_string("HELLO")).number == true);
1954
- assert(not_equal(new_excel_string("1"), ONE).number == true);
1955
- assert(not_equal(DIV0, ONE).type == ExcelError);
1956
-
1957
- // Test excel_if
1958
- // Two argument version
1959
- assert(excel_if_2(TRUE,new_excel_number(10)).type == ExcelNumber);
1960
- assert(excel_if_2(TRUE,new_excel_number(10)).number == 10);
1961
- assert(excel_if_2(FALSE,new_excel_number(10)).type == ExcelBoolean);
1962
- assert(excel_if_2(FALSE,new_excel_number(10)).number == false);
1963
- assert(excel_if_2(NA,new_excel_number(10)).type == ExcelError);
1964
- // Three argument version
1965
- assert(excel_if(TRUE,new_excel_number(10),new_excel_number(20)).type == ExcelNumber);
1966
- assert(excel_if(TRUE,new_excel_number(10),new_excel_number(20)).number == 10);
1967
- assert(excel_if(FALSE,new_excel_number(10),new_excel_number(20)).type == ExcelNumber);
1968
- assert(excel_if(FALSE,new_excel_number(10),new_excel_number(20)).number == 20);
1969
- assert(excel_if(NA,new_excel_number(10),new_excel_number(20)).type == ExcelError);
1970
- assert(excel_if(TRUE,new_excel_number(10),NA).type == ExcelNumber);
1971
- assert(excel_if(TRUE,new_excel_number(10),NA).number == 10);
1972
-
1973
- // Test excel_match
1974
- ExcelValue excel_match_array_1[] = { new_excel_number(10), new_excel_number(100) };
1975
- ExcelValue excel_match_array_1_v = new_excel_range(excel_match_array_1,1,2);
1976
- ExcelValue excel_match_array_2[] = { new_excel_string("Pear"), new_excel_string("Bear"), new_excel_string("Apple") };
1977
- ExcelValue excel_match_array_2_v = new_excel_range(excel_match_array_2,3,1);
1978
- ExcelValue excel_match_array_4[] = { ONE, BLANK, new_excel_number(0) };
1979
- ExcelValue excel_match_array_4_v = new_excel_range(excel_match_array_4,1,3);
1980
- ExcelValue excel_match_array_5[] = { ONE, new_excel_number(0), BLANK };
1981
- ExcelValue excel_match_array_5_v = new_excel_range(excel_match_array_5,1,3);
1982
-
1983
- // Two argument version
1984
- assert(excel_match_2(new_excel_number(10),excel_match_array_1_v).number == 1);
1985
- assert(excel_match_2(new_excel_number(100),excel_match_array_1_v).number == 2);
1986
- assert(excel_match_2(new_excel_number(1000),excel_match_array_1_v).type == ExcelError);
1987
- assert(excel_match_2(new_excel_number(0), excel_match_array_4_v).number == 2);
1988
- assert(excel_match_2(BLANK, excel_match_array_5_v).number == 2);
1989
-
1990
- // Three argument version
1991
- assert(excel_match(new_excel_number(10.0), excel_match_array_1_v, new_excel_number(0) ).number == 1);
1992
- assert(excel_match(new_excel_number(100.0), excel_match_array_1_v, new_excel_number(0) ).number == 2);
1993
- assert(excel_match(new_excel_number(1000.0), excel_match_array_1_v, new_excel_number(0) ).type == ExcelError);
1994
- assert(excel_match(new_excel_string("bEAr"), excel_match_array_2_v, new_excel_number(0) ).number == 2);
1995
- assert(excel_match(new_excel_number(1000.0), excel_match_array_1_v, ONE ).number == 2);
1996
- assert(excel_match(new_excel_number(1.0), excel_match_array_1_v, ONE ).type == ExcelError);
1997
- assert(excel_match(new_excel_string("Care"), excel_match_array_2_v, new_excel_number(-1) ).number == 1 );
1998
- assert(excel_match(new_excel_string("Zebra"), excel_match_array_2_v, new_excel_number(-1) ).type == ExcelError);
1999
- assert(excel_match(new_excel_string("a"), excel_match_array_2_v, new_excel_number(-1) ).number == 2);
2000
-
2001
- // When not given a range
2002
- assert(excel_match(new_excel_number(10.0), new_excel_number(10), new_excel_number(0.0)).number == 1);
2003
- assert(excel_match(new_excel_number(20.0), new_excel_number(10), new_excel_number(0.0)).type == ExcelError);
2004
- assert(excel_match(new_excel_number(10.0), excel_match_array_1_v, BLANK).number == 1);
2005
-
2006
- // Test more than on
2007
- // .. numbers
2008
- assert(more_than(ONE,new_excel_number(2)).number == false);
2009
- assert(more_than(ONE,ONE).number == false);
2010
- assert(more_than(ONE,new_excel_number(0)).number == true);
2011
- // .. booleans
2012
- assert(more_than(FALSE,FALSE).number == false);
2013
- assert(more_than(FALSE,TRUE).number == false);
2014
- assert(more_than(TRUE,FALSE).number == true);
2015
- assert(more_than(TRUE,TRUE).number == false);
2016
- // ..strings
2017
- assert(more_than(new_excel_string("HELLO"),new_excel_string("Ardvark")).number == true);
2018
- assert(more_than(new_excel_string("HELLO"),new_excel_string("world")).number == false);
2019
- assert(more_than(new_excel_string("HELLO"),new_excel_string("hello")).number == false);
2020
- // ..blanks
2021
- assert(more_than(BLANK,ONE).number == false);
2022
- assert(more_than(BLANK,new_excel_number(-1)).number == true);
2023
- assert(more_than(ONE,BLANK).number == true);
2024
- assert(more_than(new_excel_number(-1),BLANK).number == false);
2025
-
2026
- // Test less than on
2027
- // .. numbers
2028
- assert(less_than(ONE,new_excel_number(2)).number == true);
2029
- assert(less_than(ONE,ONE).number == false);
2030
- assert(less_than(ONE,new_excel_number(0)).number == false);
2031
- // .. booleans
2032
- assert(less_than(FALSE,FALSE).number == false);
2033
- assert(less_than(FALSE,TRUE).number == true);
2034
- assert(less_than(TRUE,FALSE).number == false);
2035
- assert(less_than(TRUE,TRUE).number == false);
2036
- // ..strings
2037
- assert(less_than(new_excel_string("HELLO"),new_excel_string("Ardvark")).number == false);
2038
- assert(less_than(new_excel_string("HELLO"),new_excel_string("world")).number == true);
2039
- assert(less_than(new_excel_string("HELLO"),new_excel_string("hello")).number == false);
2040
- // ..blanks
2041
- assert(less_than(BLANK,ONE).number == true);
2042
- assert(less_than(BLANK,new_excel_number(-1)).number == false);
2043
- assert(less_than(ONE,BLANK).number == false);
2044
- assert(less_than(new_excel_number(-1),BLANK).number == true);
2045
-
2046
- // Test FIND function
2047
- // ... should find the first occurrence of one string in another, returning :value if the string doesn't match
2048
- assert(find_2(new_excel_string("one"),new_excel_string("onetwothree")).number == 1);
2049
- assert(find_2(new_excel_string("one"),new_excel_string("twoonethree")).number == 4);
2050
- assert(find_2(new_excel_string("one"),new_excel_string("twoonthree")).type == ExcelError);
2051
- // ... should find the first occurrence of one string in another after a given index, returning :value if the string doesn't match
2052
- assert(find(new_excel_string("one"),new_excel_string("onetwothree"),ONE).number == 1);
2053
- assert(find(new_excel_string("one"),new_excel_string("twoonethree"),new_excel_number(5)).type == ExcelError);
2054
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_number(2)).number == 4);
2055
- // ... should be possible for the start_num to be a string, if that string converts to a number
2056
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_string("2")).number == 4);
2057
- // ... should return a :value error when given anything but a number as the third argument
2058
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_string("a")).type == ExcelError);
2059
- // ... should return a :value error when given a third argument that is less than 1 or greater than the length of the string
2060
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_number(0)).type == ExcelError);
2061
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_number(-1)).type == ExcelError);
2062
- assert(find(new_excel_string("one"),new_excel_string("oneone"),new_excel_number(7)).type == ExcelError);
2063
- // ... BLANK in the first argument matches any character
2064
- assert(find_2(BLANK,new_excel_string("abcdefg")).number == 1);
2065
- assert(find(BLANK,new_excel_string("abcdefg"),new_excel_number(4)).number == 4);
2066
- // ... should treat BLANK in the second argument as an empty string
2067
- assert(find_2(BLANK,BLANK).number == 1);
2068
- assert(find_2(new_excel_string("a"),BLANK).type == ExcelError);
2069
- // ... should return an error if any argument is an error
2070
- assert(find(new_excel_string("one"),new_excel_string("onetwothree"),NA).type == ExcelError);
2071
- assert(find(new_excel_string("one"),NA,ONE).type == ExcelError);
2072
- assert(find(NA,new_excel_string("onetwothree"),ONE).type == ExcelError);
2073
-
2074
- // Test the IFERROR function
2075
- assert(iferror(new_excel_string("ok"),ONE).type == ExcelString);
2076
- assert(iferror(VALUE,ONE).type == ExcelNumber);
2077
-
2078
- // Test the INDEX function
2079
- ExcelValue index_array_1[] = { new_excel_number(10), new_excel_number(20), BLANK };
2080
- ExcelValue index_array_1_v_column = new_excel_range(index_array_1,3,1);
2081
- ExcelValue index_array_1_v_row = new_excel_range(index_array_1,1,3);
2082
- ExcelValue index_array_2[] = { BLANK, ONE, new_excel_number(10), new_excel_number(11), new_excel_number(100), new_excel_number(101) };
2083
- ExcelValue index_array_2_v = new_excel_range(index_array_2,3,2);
2084
- // ... if given one argument should return the value at that offset in the range
2085
- assert(excel_index_2(index_array_1_v_column,new_excel_number(2.0)).number == 20);
2086
- assert(excel_index_2(index_array_1_v_row,new_excel_number(2.0)).number == 20);
2087
- // ... but not if the range is not a single row or single column
2088
- assert(excel_index_2(index_array_2_v,new_excel_number(2.0)).type == ExcelError);
2089
- // ... it should return the value in the array at position row_number, column_number
2090
- assert(excel_index(new_excel_number(10),ONE,ONE).number == 10);
2091
- assert(excel_index(index_array_2_v,new_excel_number(1.0),new_excel_number(2.0)).number == 1);
2092
- assert(excel_index(index_array_2_v,new_excel_number(2.0),new_excel_number(1.0)).number == 10);
2093
- assert(excel_index(index_array_2_v,new_excel_number(3.0),new_excel_number(1.0)).number == 100);
2094
- assert(excel_index(index_array_2_v,new_excel_number(3.0),new_excel_number(3.0)).type == ExcelError);
2095
- // ... it should return ZERO not blank, if a blank cell is picked
2096
- assert(excel_index(index_array_2_v,new_excel_number(1.0),new_excel_number(1.0)).type == ExcelNumber);
2097
- assert(excel_index(index_array_2_v,new_excel_number(1.0),new_excel_number(1.0)).number == 0);
2098
- assert(excel_index_2(index_array_1_v_row,new_excel_number(3.0)).type == ExcelNumber);
2099
- assert(excel_index_2(index_array_1_v_row,new_excel_number(3.0)).number == 0);
2100
- // ... it should return the whole row if given a zero column number
2101
- ExcelValue index_result_1_v = excel_index(index_array_2_v,new_excel_number(1.0),new_excel_number(0.0));
2102
- assert(index_result_1_v.type == ExcelRange);
2103
- assert(index_result_1_v.rows == 1);
2104
- assert(index_result_1_v.columns == 2);
2105
- ExcelValue *index_result_1_a = index_result_1_v.array;
2106
- assert(index_result_1_a[0].number == 0);
2107
- assert(index_result_1_a[1].number == 1);
2108
- // ... it should return the whole column if given a zero row number
2109
- ExcelValue index_result_2_v = excel_index(index_array_2_v,new_excel_number(0),new_excel_number(1.0));
2110
- assert(index_result_2_v.type == ExcelRange);
2111
- assert(index_result_2_v.rows == 3);
2112
- assert(index_result_2_v.columns == 1);
2113
- ExcelValue *index_result_2_a = index_result_2_v.array;
2114
- assert(index_result_2_a[0].number == 0);
2115
- assert(index_result_2_a[1].number == 10);
2116
- assert(index_result_2_a[2].number == 100);
2117
- // ... it should return a :ref error when given arguments outside array range
2118
- assert(excel_index_2(index_array_1_v_row,new_excel_number(-1)).type == ExcelError);
2119
- assert(excel_index_2(index_array_1_v_row,new_excel_number(4)).type == ExcelError);
2120
- // ... it should treat BLANK as zero if given as a required row or column number
2121
- assert(excel_index(index_array_2_v,new_excel_number(1.0),BLANK).type == ExcelRange);
2122
- assert(excel_index(index_array_2_v,BLANK,new_excel_number(2.0)).type == ExcelRange);
2123
- // ... it should return an error if an argument is an error
2124
- assert(excel_index(NA,NA,NA).type == ExcelError);
2125
-
2126
- // LEFT(string,[characters])
2127
- // ... should return the left n characters from a string
2128
- assert(strcmp(left_1(new_excel_string("ONE")).string,"O") == 0);
2129
- assert(strcmp(left(new_excel_string("ONE"),ONE).string,"O") == 0);
2130
- assert(strcmp(left(new_excel_string("ONE"),new_excel_number(3)).string,"ONE") == 0);
2131
- // ... should turn numbers into strings before processing
2132
- assert(strcmp(left(new_excel_number(1.31e12),new_excel_number(3)).string, "131") == 0);
2133
- // ... should turn booleans into the words TRUE and FALSE before processing
2134
- assert(strcmp(left(TRUE,new_excel_number(3)).string,"TRU") == 0);
2135
- assert(strcmp(left(FALSE,new_excel_number(3)).string,"FAL") == 0);
2136
- // ... should return BLANK if given BLANK for either argument
2137
- assert(left(BLANK,new_excel_number(3)).type == ExcelEmpty);
2138
- assert(left(new_excel_string("ONE"),BLANK).type == ExcelEmpty);
2139
- // ... should return an error if an argument is an error
2140
- assert(left_1(NA).type == ExcelError);
2141
- assert(left(new_excel_string("ONE"),NA).type == ExcelError);
2142
-
2143
- // Test less than or equal to
2144
- // .. numbers
2145
- assert(less_than_or_equal(ONE,new_excel_number(2)).number == true);
2146
- assert(less_than_or_equal(ONE,ONE).number == true);
2147
- assert(less_than_or_equal(ONE,new_excel_number(0)).number == false);
2148
- // .. booleans
2149
- assert(less_than_or_equal(FALSE,FALSE).number == true);
2150
- assert(less_than_or_equal(FALSE,TRUE).number == true);
2151
- assert(less_than_or_equal(TRUE,FALSE).number == false);
2152
- assert(less_than_or_equal(TRUE,TRUE).number == true);
2153
- // ..strings
2154
- assert(less_than_or_equal(new_excel_string("HELLO"),new_excel_string("Ardvark")).number == false);
2155
- assert(less_than_or_equal(new_excel_string("HELLO"),new_excel_string("world")).number == true);
2156
- assert(less_than_or_equal(new_excel_string("HELLO"),new_excel_string("hello")).number == true);
2157
- // ..blanks
2158
- assert(less_than_or_equal(BLANK,ONE).number == true);
2159
- assert(less_than_or_equal(BLANK,new_excel_number(-1)).number == false);
2160
- assert(less_than_or_equal(ONE,BLANK).number == false);
2161
- assert(less_than_or_equal(new_excel_number(-1),BLANK).number == true);
2162
-
2163
- // Test MAX
2164
- assert(max(4, array1).number == 10);
2165
- assert(max(3, array2).number == 10);
2166
- assert(max(4, array3).type == ExcelError);
2167
-
2168
- // Test MIN
2169
- assert(min(4, array1).number == 5);
2170
- assert(min(3, array2).number == 5);
2171
- assert(min(4, array3).type == ExcelError);
2172
-
2173
- // Test MOD
2174
- // ... should return the remainder of a number
2175
- assert(mod(new_excel_number(10), new_excel_number(3)).number == 1.0);
2176
- assert(mod(new_excel_number(10), new_excel_number(5)).number == 0.0);
2177
- // ... should be possible for the the arguments to be strings, if they convert to a number
2178
- assert(mod(new_excel_string("3.5"),new_excel_string("2")).number == 1.5);
2179
- // ... should treat BLANK as zero
2180
- assert(mod(BLANK,new_excel_number(10)).number == 0);
2181
- assert(mod(new_excel_number(10),BLANK).type == ExcelError);
2182
- assert(mod(BLANK,BLANK).type == ExcelError);
2183
- // ... should treat true as 1 and FALSE as 0
2184
- assert((mod(new_excel_number(1.1),TRUE).number - 0.1) < 0.001);
2185
- assert(mod(new_excel_number(1.1),FALSE).type == ExcelError);
2186
- assert(mod(FALSE,new_excel_number(10)).number == 0);
2187
- // ... should return an error when given inappropriate arguments
2188
- assert(mod(new_excel_string("Asdasddf"),new_excel_string("adsfads")).type == ExcelError);
2189
- // ... should return an error if an argument is an error
2190
- assert(mod(new_excel_number(1),VALUE).type == ExcelError);
2191
- assert(mod(VALUE,new_excel_number(1)).type == ExcelError);
2192
- assert(mod(VALUE,VALUE).type == ExcelError);
2193
-
2194
- // Test more than or equal to on
2195
- // .. numbers
2196
- assert(more_than_or_equal(ONE,new_excel_number(2)).number == false);
2197
- assert(more_than_or_equal(ONE,ONE).number == true);
2198
- assert(more_than_or_equal(ONE,new_excel_number(0)).number == true);
2199
- // .. booleans
2200
- assert(more_than_or_equal(FALSE,FALSE).number == true);
2201
- assert(more_than_or_equal(FALSE,TRUE).number == false);
2202
- assert(more_than_or_equal(TRUE,FALSE).number == true);
2203
- assert(more_than_or_equal(TRUE,TRUE).number == true);
2204
- // ..strings
2205
- assert(more_than_or_equal(new_excel_string("HELLO"),new_excel_string("Ardvark")).number == true);
2206
- assert(more_than_or_equal(new_excel_string("HELLO"),new_excel_string("world")).number == false);
2207
- assert(more_than_or_equal(new_excel_string("HELLO"),new_excel_string("hello")).number == true);
2208
- // ..blanks
2209
- assert(more_than_or_equal(BLANK,BLANK).number == true);
2210
- assert(more_than_or_equal(BLANK,ONE).number == false);
2211
- assert(more_than_or_equal(BLANK,new_excel_number(-1)).number == true);
2212
- assert(more_than_or_equal(ONE,BLANK).number == true);
2213
- assert(more_than_or_equal(new_excel_number(-1),BLANK).number == false);
2214
-
2215
- // Test negative
2216
- // ... should return the negative of its arguments
2217
- assert(negative(new_excel_number(1)).number == -1);
2218
- assert(negative(new_excel_number(-1)).number == 1);
2219
- // ... should treat strings that only contain numbers as numbers
2220
- assert(negative(new_excel_string("10")).number == -10);
2221
- assert(negative(new_excel_string("-1.3")).number == 1.3);
2222
- // ... should return an error when given inappropriate arguments
2223
- assert(negative(new_excel_string("Asdasddf")).type == ExcelError);
2224
- // ... should treat BLANK as zero
2225
- assert(negative(BLANK).number == 0);
2226
-
2227
- // Test PMT(rate,number_of_periods,present_value) - optional arguments not yet implemented
2228
- // ... should calculate the monthly payment required for a given principal, interest rate and loan period
2229
- assert((pmt(new_excel_number(0.1),new_excel_number(10),new_excel_number(100)).number - -16.27) < 0.01);
2230
- assert((pmt(new_excel_number(0.0123),new_excel_number(99.1),new_excel_number(123.32)).number - -2.159) < 0.01);
2231
- assert((pmt(new_excel_number(0),new_excel_number(2),new_excel_number(10)).number - -5) < 0.01);
2232
-
2233
- // Test power
2234
- // ... should return power of its arguments
2235
- assert(power(new_excel_number(2),new_excel_number(3)).number == 8);
2236
- assert(power(new_excel_number(4.0),new_excel_number(0.5)).number == 2.0);
2237
- assert(power(new_excel_number(-4.0),new_excel_number(0.5)).type == ExcelError);
2238
-
2239
- // Test round
2240
- assert(excel_round(new_excel_number(1.1), new_excel_number(0)).number == 1.0);
2241
- assert(excel_round(new_excel_number(1.5), new_excel_number(0)).number == 2.0);
2242
- assert(excel_round(new_excel_number(1.56),new_excel_number(1)).number == 1.6);
2243
- assert(excel_round(new_excel_number(-1.56),new_excel_number(1)).number == -1.6);
2244
-
2245
- // Test rounddown
2246
- assert(rounddown(new_excel_number(1.1), new_excel_number(0)).number == 1.0);
2247
- assert(rounddown(new_excel_number(1.5), new_excel_number(0)).number == 1.0);
2248
- assert(rounddown(new_excel_number(1.56),new_excel_number(1)).number == 1.5);
2249
- assert(rounddown(new_excel_number(-1.56),new_excel_number(1)).number == -1.5);
2250
-
2251
- // Test int
2252
- assert(excel_int(new_excel_number(8.9)).number == 8.0);
2253
- assert(excel_int(new_excel_number(-8.9)).number == -9.0);
2254
-
2255
- // Test roundup
2256
- assert(roundup(new_excel_number(1.1), new_excel_number(0)).number == 2.0);
2257
- assert(roundup(new_excel_number(1.5), new_excel_number(0)).number == 2.0);
2258
- assert(roundup(new_excel_number(1.56),new_excel_number(1)).number == 1.6);
2259
- assert(roundup(new_excel_number(-1.56),new_excel_number(1)).number == -1.6);
2260
-
2261
- // Test string joining
2262
- ExcelValue string_join_array_1[] = {new_excel_string("Hello "), new_excel_string("world")};
2263
- ExcelValue string_join_array_2[] = {new_excel_string("Hello "), new_excel_string("world"), new_excel_string("!")};
2264
- ExcelValue string_join_array_3[] = {new_excel_string("Top "), new_excel_number(10.0)};
2265
- ExcelValue string_join_array_4[] = {new_excel_string("Top "), new_excel_number(10.5)};
2266
- ExcelValue string_join_array_5[] = {new_excel_string("Top "), TRUE, FALSE};
2267
- // ... should return a string by combining its arguments
2268
- // inspect_excel_value(string_join(2, string_join_array_1));
2269
- assert(string_join(2, string_join_array_1).string[6] == 'w');
2270
- assert(string_join(2, string_join_array_1).string[11] == '\0');
2271
- // ... should cope with an arbitrary number of arguments
2272
- assert(string_join(3, string_join_array_2).string[11] == '!');
2273
- assert(string_join(3, string_join_array_3).string[12] == '\0');
2274
- // ... should convert values to strings as it goes
2275
- assert(string_join(2, string_join_array_3).string[4] == '1');
2276
- assert(string_join(2, string_join_array_3).string[5] == '0');
2277
- assert(string_join(2, string_join_array_3).string[6] == '\0');
2278
- // ... should convert integer values into strings without decimal points, and float values with decimal points
2279
- assert(string_join(2, string_join_array_4).string[4] == '1');
2280
- assert(string_join(2, string_join_array_4).string[5] == '0');
2281
- assert(string_join(2, string_join_array_4).string[6] == '.');
2282
- assert(string_join(2, string_join_array_4).string[7] == '5');
2283
- assert(string_join(2, string_join_array_4).string[8] == '\0');
2284
- // ... should convert TRUE and FALSE into strings
2285
- assert(string_join(3,string_join_array_5).string[4] == 'T');
2286
- // Should deal with very long string joins
2287
- ExcelValue string_join_array_6[] = {new_excel_string("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"), new_excel_string("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")};
2288
- assert(string_join(2, string_join_array_6).string[0] == '0');
2289
- free_all_allocated_memory();
2290
-
2291
- // Test SUBTOTAL function
2292
- ExcelValue subtotal_array_1[] = {new_excel_number(10),new_excel_number(100),BLANK};
2293
- ExcelValue subtotal_array_1_v = new_excel_range(subtotal_array_1,3,1);
2294
- ExcelValue subtotal_array_2[] = {new_excel_number(1),new_excel_string("two"),subtotal_array_1_v};
2295
-
2296
- // new_excel_number(1.0);
2297
- // inspect_excel_value(new_excel_number(1.0));
2298
- // inspect_excel_value(new_excel_range(subtotal_array_2,3,1));
2299
- // inspect_excel_value(subtotal(new_excel_number(1.0),3,subtotal_array_2));
2300
-
2301
- assert(subtotal(new_excel_number(1.0),3,subtotal_array_2).number == 111.0/3.0);
2302
- assert(subtotal(new_excel_number(2.0),3,subtotal_array_2).number == 3);
2303
- assert(subtotal(new_excel_number(3.0),7, count_a_test_array_1).number == 6);
2304
- assert(subtotal(new_excel_number(3.0),3,subtotal_array_2).number == 4);
2305
- assert(subtotal(new_excel_number(9.0),3,subtotal_array_2).number == 111);
2306
- assert(subtotal(new_excel_number(101.0),3,subtotal_array_2).number == 111.0/3.0);
2307
- assert(subtotal(new_excel_number(102.0),3,subtotal_array_2).number == 3);
2308
- assert(subtotal(new_excel_number(103.0),3,subtotal_array_2).number == 4);
2309
- assert(subtotal(new_excel_number(109.0),3,subtotal_array_2).number == 111);
2310
-
2311
- // Test SUMIFS function
2312
- ExcelValue sumifs_array_1[] = {new_excel_number(10),new_excel_number(100),BLANK};
2313
- ExcelValue sumifs_array_1_v = new_excel_range(sumifs_array_1,3,1);
2314
- ExcelValue sumifs_array_2[] = {new_excel_string("pear"),new_excel_string("bear"),new_excel_string("apple")};
2315
- ExcelValue sumifs_array_2_v = new_excel_range(sumifs_array_2,3,1);
2316
- ExcelValue sumifs_array_3[] = {new_excel_number(1),new_excel_number(2),new_excel_number(3),new_excel_number(4),new_excel_number(5),new_excel_number(5)};
2317
- ExcelValue sumifs_array_3_v = new_excel_range(sumifs_array_3,6,1);
2318
- ExcelValue sumifs_array_4[] = {new_excel_string("CO2"),new_excel_string("CH4"),new_excel_string("N2O"),new_excel_string("CH4"),new_excel_string("N2O"),new_excel_string("CO2")};
2319
- ExcelValue sumifs_array_4_v = new_excel_range(sumifs_array_4,6,1);
2320
- ExcelValue sumifs_array_5[] = {new_excel_string("1A"),new_excel_string("1A"),new_excel_string("1A"),new_excel_number(4),new_excel_number(4),new_excel_number(5)};
2321
- ExcelValue sumifs_array_5_v = new_excel_range(sumifs_array_5,6,1);
2322
-
2323
- // ... should only sum values that meet all of the criteria
2324
- ExcelValue sumifs_array_6[] = { sumifs_array_1_v, new_excel_number(10), sumifs_array_2_v, new_excel_string("Bear") };
2325
- assert(sumifs(sumifs_array_1_v,4,sumifs_array_6).number == 0.0);
2326
-
2327
- ExcelValue sumifs_array_7[] = { sumifs_array_1_v, new_excel_number(10), sumifs_array_2_v, new_excel_string("Pear") };
2328
- assert(sumifs(sumifs_array_1_v,4,sumifs_array_7).number == 10.0);
2329
-
2330
- // ... should work when single cells are given where ranges expected
2331
- ExcelValue sumifs_array_8[] = { new_excel_string("CAR"), new_excel_string("CAR"), new_excel_string("FCV"), new_excel_string("FCV")};
2332
- assert(sumifs(new_excel_number(0.143897265452564), 4, sumifs_array_8).number == 0.143897265452564);
2333
-
2334
- // ... should match numbers with strings that contain numbers
2335
- ExcelValue sumifs_array_9[] = { new_excel_number(10), new_excel_string("10.0")};
2336
- assert(sumifs(new_excel_number(100),2,sumifs_array_9).number == 100);
2337
-
2338
- ExcelValue sumifs_array_10[] = { sumifs_array_4_v, new_excel_string("CO2"), sumifs_array_5_v, new_excel_number(2)};
2339
- assert(sumifs(sumifs_array_3_v,4, sumifs_array_10).number == 0);
2340
-
2341
- // ... should match with strings that contain criteria
2342
- ExcelValue sumifs_array_10a[] = { sumifs_array_3_v, new_excel_string("=5")};
2343
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10a).number == 10);
2344
-
2345
- ExcelValue sumifs_array_10b[] = { sumifs_array_3_v, new_excel_string("<>3")};
2346
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10b).number == 17);
2347
-
2348
- ExcelValue sumifs_array_10c[] = { sumifs_array_3_v, new_excel_string("<3")};
2349
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10c).number == 3);
2350
-
2351
- ExcelValue sumifs_array_10d[] = { sumifs_array_3_v, new_excel_string("<=3")};
2352
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10d).number == 6);
2353
-
2354
- ExcelValue sumifs_array_10e[] = { sumifs_array_3_v, new_excel_string(">3")};
2355
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10e).number == 14);
2356
-
2357
- ExcelValue sumifs_array_10f[] = { sumifs_array_3_v, new_excel_string(">=3")};
2358
- assert(sumifs(sumifs_array_3_v,2, sumifs_array_10f).number == 17);
2359
-
2360
- // ... should treat BLANK as an empty string when in the check_range, but not in the criteria
2361
- ExcelValue sumifs_array_11[] = { BLANK, new_excel_number(20)};
2362
- assert(sumifs(new_excel_number(100),2,sumifs_array_11).number == 0);
2363
-
2364
- ExcelValue sumifs_array_12[] = {BLANK, new_excel_string("")};
2365
- assert(sumifs(new_excel_number(100),2,sumifs_array_12).number == 100);
2366
-
2367
- ExcelValue sumifs_array_13[] = {BLANK, BLANK};
2368
- assert(sumifs(new_excel_number(100),2,sumifs_array_13).number == 0);
2369
-
2370
- // ... should return an error if range argument is an error
2371
- assert(sumifs(REF,2,sumifs_array_13).type == ExcelError);
2372
-
2373
-
2374
- // Test SUMIF
2375
- // ... where there is only a check range
2376
- assert(sumif_2(sumifs_array_1_v,new_excel_string(">0")).number == 110.0);
2377
- assert(sumif_2(sumifs_array_1_v,new_excel_string(">10")).number == 100.0);
2378
- assert(sumif_2(sumifs_array_1_v,new_excel_string("<100")).number == 10.0);
2379
-
2380
- // ... where there is a seprate sum range
2381
- ExcelValue sumif_array_1[] = {new_excel_number(15),new_excel_number(20), new_excel_number(30)};
2382
- ExcelValue sumif_array_1_v = new_excel_range(sumif_array_1,3,1);
2383
- assert(sumif(sumifs_array_1_v,new_excel_string("10"),sumif_array_1_v).number == 15);
2384
-
2385
-
2386
- // Test SUMPRODUCT
2387
- ExcelValue sumproduct_1[] = { new_excel_number(10), new_excel_number(100), BLANK};
2388
- ExcelValue sumproduct_2[] = { BLANK, new_excel_number(100), new_excel_number(10), BLANK};
2389
- ExcelValue sumproduct_3[] = { BLANK };
2390
- ExcelValue sumproduct_4[] = { new_excel_number(10), new_excel_number(100), new_excel_number(1000)};
2391
- ExcelValue sumproduct_5[] = { new_excel_number(1), new_excel_number(2), new_excel_number(3)};
2392
- ExcelValue sumproduct_6[] = { new_excel_number(1), new_excel_number(2), new_excel_number(4), new_excel_number(5)};
2393
- ExcelValue sumproduct_7[] = { new_excel_number(10), new_excel_number(20), new_excel_number(40), new_excel_number(50)};
2394
- ExcelValue sumproduct_8[] = { new_excel_number(11), new_excel_number(21), new_excel_number(41), new_excel_number(51)};
2395
- ExcelValue sumproduct_9[] = { BLANK, BLANK };
2396
-
2397
- ExcelValue sumproduct_1_v = new_excel_range( sumproduct_1, 3, 1);
2398
- ExcelValue sumproduct_2_v = new_excel_range( sumproduct_2, 3, 1);
2399
- ExcelValue sumproduct_3_v = new_excel_range( sumproduct_3, 1, 1);
2400
- // ExcelValue sumproduct_4_v = new_excel_range( sumproduct_4, 1, 3); // Unused
2401
- ExcelValue sumproduct_5_v = new_excel_range( sumproduct_5, 3, 1);
2402
- ExcelValue sumproduct_6_v = new_excel_range( sumproduct_6, 2, 2);
2403
- ExcelValue sumproduct_7_v = new_excel_range( sumproduct_7, 2, 2);
2404
- ExcelValue sumproduct_8_v = new_excel_range( sumproduct_8, 2, 2);
2405
- ExcelValue sumproduct_9_v = new_excel_range( sumproduct_9, 2, 1);
2406
-
2407
- // ... should multiply together and then sum the elements in row or column areas given as arguments
2408
- ExcelValue sumproducta_1[] = {sumproduct_1_v, sumproduct_2_v};
2409
- assert(sumproduct(2,sumproducta_1).number == 100*100);
2410
-
2411
- // ... should return :value when miss-matched array sizes
2412
- ExcelValue sumproducta_2[] = {sumproduct_1_v, sumproduct_3_v};
2413
- assert(sumproduct(2,sumproducta_2).type == ExcelError);
2414
-
2415
- // ... if all its arguments are single values, should multiply them together
2416
- // ExcelValue *sumproducta_3 = sumproduct_4;
2417
- assert(sumproduct(3,sumproduct_4).number == 10*100*1000);
2418
-
2419
- // ... if it only has one range as an argument, should add its elements together
2420
- ExcelValue sumproducta_4[] = {sumproduct_5_v};
2421
- assert(sumproduct(1,sumproducta_4).number == 1 + 2 + 3);
2422
-
2423
- // ... if given multi row and column areas as arguments, should multipy the corresponding cell in each area and then add them all
2424
- ExcelValue sumproducta_5[] = {sumproduct_6_v, sumproduct_7_v, sumproduct_8_v};
2425
- assert(sumproduct(3,sumproducta_5).number == 1*10*11 + 2*20*21 + 4*40*41 + 5*50*51);
2426
-
2427
- // ... should raise an error if BLANK values outside of an array
2428
- ExcelValue sumproducta_6[] = {BLANK,new_excel_number(1)};
2429
- assert(sumproduct(2,sumproducta_6).type == ExcelError);
2430
-
2431
- // ... should ignore non-numeric values within an array
2432
- ExcelValue sumproducta_7[] = {sumproduct_9_v, sumproduct_9_v};
2433
- assert(sumproduct(2,sumproducta_7).number == 0);
2434
-
2435
- // ... should return an error if an argument is an error
2436
- ExcelValue sumproducta_8[] = {VALUE};
2437
- assert(sumproduct(1,sumproducta_8).type == ExcelError);
2438
-
2439
- // Test VLOOKUP
2440
- ExcelValue vlookup_a1[] = {new_excel_number(1),new_excel_number(10),new_excel_number(2),new_excel_number(20),new_excel_number(3),new_excel_number(30)};
2441
- ExcelValue vlookup_a2[] = {new_excel_string("hello"),new_excel_number(10),new_excel_number(2),new_excel_number(20),new_excel_number(3),new_excel_number(30)};
2442
- ExcelValue vlookup_a3[] = {BLANK,new_excel_number(10),new_excel_number(2),new_excel_number(20),new_excel_number(3),new_excel_number(30)};
2443
- ExcelValue vlookup_a1_v = new_excel_range(vlookup_a1,3,2);
2444
- ExcelValue vlookup_a2_v = new_excel_range(vlookup_a2,3,2);
2445
- ExcelValue vlookup_a3_v = new_excel_range(vlookup_a3,3,2);
2446
- // ... should match the first argument against the first column of the table in the second argument, returning the value in the column specified by the third argument
2447
- assert(vlookup_3(new_excel_number(2.0),vlookup_a1_v,new_excel_number(2)).number == 20);
2448
- assert(vlookup_3(new_excel_number(1.5),vlookup_a1_v,new_excel_number(2)).number == 10);
2449
- assert(vlookup_3(new_excel_number(0.5),vlookup_a1_v,new_excel_number(2)).type == ExcelError);
2450
- assert(vlookup_3(new_excel_number(10),vlookup_a1_v,new_excel_number(2)).number == 30);
2451
- assert(vlookup_3(new_excel_number(2.6),vlookup_a1_v,new_excel_number(2)).number == 20);
2452
- // ... has a four argument variant that matches the lookup type
2453
- assert(vlookup(new_excel_number(2.6),vlookup_a1_v,new_excel_number(2),TRUE).number == 20);
2454
- assert(vlookup(new_excel_number(2.6),vlookup_a1_v,new_excel_number(2),FALSE).type == ExcelError);
2455
- assert(vlookup(new_excel_string("HELLO"),vlookup_a2_v,new_excel_number(2),FALSE).number == 10);
2456
- assert(vlookup(new_excel_string("HELMP"),vlookup_a2_v,new_excel_number(2),TRUE).number == 10);
2457
- // .. the four argument variant should accept 0 and 1 instead of TRUE and FALSE
2458
- assert(vlookup(new_excel_string("HELLO"),vlookup_a2_v,new_excel_number(2),ZERO).number == 10);
2459
- assert(vlookup(new_excel_string("HELMP"),vlookup_a2_v,new_excel_number(2),ONE).number == 10);
2460
- // ... BLANK should not match with anything" do
2461
- assert(vlookup_3(BLANK,vlookup_a3_v,new_excel_number(2)).type == ExcelError);
2462
- // ... should return an error if an argument is an error" do
2463
- assert(vlookup(VALUE,vlookup_a1_v,new_excel_number(2),FALSE).type == ExcelError);
2464
- assert(vlookup(new_excel_number(2.0),VALUE,new_excel_number(2),FALSE).type == ExcelError);
2465
- assert(vlookup(new_excel_number(2.0),vlookup_a1_v,VALUE,FALSE).type == ExcelError);
2466
- assert(vlookup(new_excel_number(2.0),vlookup_a1_v,new_excel_number(2),VALUE).type == ExcelError);
2467
- assert(vlookup(VALUE,VALUE,VALUE,VALUE).type == ExcelError);
2468
-
2469
- // Test HLOOKUP
2470
- ExcelValue hlookup_a1[] = {new_excel_number(1),new_excel_number(2),new_excel_number(3),new_excel_number(10),new_excel_number(20),new_excel_number(30)};
2471
- ExcelValue hlookup_a2[] = {new_excel_string("hello"),new_excel_number(2),new_excel_number(3),new_excel_number(10),new_excel_number(20),new_excel_number(30)};
2472
- ExcelValue hlookup_a3[] = {BLANK,new_excel_number(2),new_excel_number(3),new_excel_number(10),new_excel_number(20),new_excel_number(30)};
2473
- ExcelValue hlookup_a1_v = new_excel_range(hlookup_a1,2,3);
2474
- ExcelValue hlookup_a2_v = new_excel_range(hlookup_a2,2,3);
2475
- ExcelValue hlookup_a3_v = new_excel_range(hlookup_a3,2,3);
2476
- // ... should match the first argument against the first column of the table in the second argument, returning the value in the column specified by the third argument
2477
- assert(hlookup_3(new_excel_number(2.0),hlookup_a1_v,new_excel_number(2)).number == 20);
2478
- assert(hlookup_3(new_excel_number(1.5),hlookup_a1_v,new_excel_number(2)).number == 10);
2479
- assert(hlookup_3(new_excel_number(0.5),hlookup_a1_v,new_excel_number(2)).type == ExcelError);
2480
- assert(hlookup_3(new_excel_number(10),hlookup_a1_v,new_excel_number(2)).number == 30);
2481
- assert(hlookup_3(new_excel_number(2.6),hlookup_a1_v,new_excel_number(2)).number == 20);
2482
- // ... has a four argument variant that matches the lookup type
2483
- assert(hlookup(new_excel_number(2.6),hlookup_a1_v,new_excel_number(2),TRUE).number == 20);
2484
- assert(hlookup(new_excel_number(2.6),hlookup_a1_v,new_excel_number(2),FALSE).type == ExcelError);
2485
- assert(hlookup(new_excel_string("HELLO"),hlookup_a2_v,new_excel_number(2),FALSE).number == 10);
2486
- assert(hlookup(new_excel_string("HELMP"),hlookup_a2_v,new_excel_number(2),TRUE).number == 10);
2487
- // ... that four argument variant should accept 0 or 1 for the lookup type
2488
- assert(hlookup(new_excel_number(2.6),hlookup_a1_v,new_excel_number(2),ONE).number == 20);
2489
- assert(hlookup(new_excel_number(2.6),hlookup_a1_v,new_excel_number(2),ZERO).type == ExcelError);
2490
- assert(hlookup(new_excel_string("HELLO"),hlookup_a2_v,new_excel_number(2),ZERO).number == 10);
2491
- assert(hlookup(new_excel_string("HELMP"),hlookup_a2_v,new_excel_number(2),ONE).number == 10);
2492
- // ... BLANK should not match with anything" do
2493
- assert(hlookup_3(BLANK,hlookup_a3_v,new_excel_number(2)).type == ExcelError);
2494
- // ... should return an error if an argument is an error" do
2495
- assert(hlookup(VALUE,hlookup_a1_v,new_excel_number(2),FALSE).type == ExcelError);
2496
- assert(hlookup(new_excel_number(2.0),VALUE,new_excel_number(2),FALSE).type == ExcelError);
2497
- assert(hlookup(new_excel_number(2.0),hlookup_a1_v,VALUE,FALSE).type == ExcelError);
2498
- assert(hlookup(new_excel_number(2.0),hlookup_a1_v,new_excel_number(2),VALUE).type == ExcelError);
2499
- assert(hlookup(VALUE,VALUE,VALUE,VALUE).type == ExcelError);
2500
-
2501
- // Test SUM
2502
- ExcelValue sum_array_0[] = {new_excel_number(1084.4557258064517),new_excel_number(32.0516914516129),new_excel_number(137.36439193548387)};
2503
- ExcelValue sum_array_0_v = new_excel_range(sum_array_0,3,1);
2504
- ExcelValue sum_array_1[] = {sum_array_0_v};
2505
- assert(sum(1,sum_array_1).number == 1253.8718091935484);
2506
-
2507
- // Test PV
2508
- assert((int) pv_3(new_excel_number(0.03), new_excel_number(12), new_excel_number(100)).number == -995);
2509
- assert((int) pv_4(new_excel_number(0.03), new_excel_number(12), new_excel_number(-100), new_excel_number(100)).number == 925);
2510
- assert((int) pv_5(new_excel_number(0.03), new_excel_number(12), new_excel_number(-100), new_excel_number(-100), new_excel_number(1)).number == 1095);
2511
-
2512
- // Test TEXT
2513
- assert(strcmp(text(new_excel_number(1.0), new_excel_string("0%")).string, "100%") == 0);
2514
- assert(strcmp(text(new_excel_string("1"), new_excel_string("0%")).string, "100%") == 0);
2515
- assert(strcmp(text(BLANK, new_excel_string("0%")).string, "0%") == 0);
2516
- assert(strcmp(text(new_excel_number(1.0), BLANK).string, "") == 0);
2517
- assert(strcmp(text(new_excel_string("ASGASD"), new_excel_string("0%")).string, "ASGASD") == 0);
2518
-
2519
- // Test LOG
2520
- // One argument variant assumes LOG base 10
2521
- assert(excel_log(new_excel_number(10)).number == 1);
2522
- assert(excel_log(new_excel_number(100)).number == 2);
2523
- assert(excel_log(new_excel_number(0)).type == ExcelError);
2524
- // Two argument variant allows LOG base to be specified
2525
- assert(excel_log_2(new_excel_number(8),new_excel_number(2)).number == 3.0);
2526
- assert(excel_log_2(new_excel_number(8),new_excel_number(0)).type == ExcelError);
2527
-
2528
- // Test MMULT (Matrix multiplication)
2529
- ExcelValue mmult_1[] = { ONE, TWO, THREE, FOUR};
2530
- ExcelValue mmult_2[] = { FOUR, THREE, TWO, ONE};
2531
- ExcelValue mmult_3[] = { ONE, TWO};
2532
- ExcelValue mmult_4[] = { THREE, FOUR};
2533
- ExcelValue mmult_5[] = { ONE, BLANK, THREE, FOUR};
2534
-
2535
- ExcelValue mmult_1_v = new_excel_range( mmult_1, 2, 2);
2536
- ExcelValue mmult_2_v = new_excel_range( mmult_2, 2, 2);
2537
- ExcelValue mmult_3_v = new_excel_range( mmult_3, 1, 2);
2538
- ExcelValue mmult_4_v = new_excel_range( mmult_4, 2, 1);
2539
- ExcelValue mmult_5_v = new_excel_range( mmult_5, 2, 2);
2540
-
2541
- // Treat the ranges as matrices and multiply them
2542
- ExcelValue mmult_result_1_v = mmult(mmult_1_v, mmult_2_v);
2543
- assert(mmult_result_1_v.type == ExcelRange);
2544
- assert(mmult_result_1_v.rows == 2);
2545
- assert(mmult_result_1_v.columns == 2);
2546
- ExcelValue *mmult_result_1_a = mmult_result_1_v.array;
2547
- assert(mmult_result_1_a[0].number == 8);
2548
- assert(mmult_result_1_a[1].number == 5);
2549
- assert(mmult_result_1_a[2].number == 20);
2550
- assert(mmult_result_1_a[3].number == 13);
2551
-
2552
- ExcelValue mmult_result_2_v = mmult(mmult_3_v, mmult_4_v);
2553
- assert(mmult_result_2_v.type == ExcelRange);
2554
- assert(mmult_result_2_v.rows == 1);
2555
- assert(mmult_result_2_v.columns == 1);
2556
- ExcelValue *mmult_result_2_a = mmult_result_2_v.array;
2557
- assert(mmult_result_2_a[0].number == 11);
2558
-
2559
- // Return an error if any cells are not numbers
2560
- ExcelValue mmult_result_3_v = mmult(mmult_5_v, mmult_2_v);
2561
- assert(mmult_result_3_v.type == ExcelRange);
2562
- assert(mmult_result_3_v.rows == 2);
2563
- assert(mmult_result_3_v.columns == 2);
2564
- ExcelValue *mmult_result_3_a = mmult_result_3_v.array;
2565
- assert(mmult_result_3_a[0].type == ExcelError);
2566
- assert(mmult_result_3_a[1].type == ExcelError);
2567
- assert(mmult_result_3_a[2].type == ExcelError);
2568
- assert(mmult_result_3_a[3].type == ExcelError);
2569
-
2570
- // Returns errors if arguments are not ranges
2571
- // FIXME: Should work in edge case where passed two numbers which excel treats as ranges with one row and column
2572
- ExcelValue mmult_result_4_v = mmult(ONE, mmult_2_v);
2573
- assert(mmult_result_4_v.type == ExcelError);
2574
-
2575
- // Returns errors if the ranges aren't the right size to multiply
2576
- ExcelValue mmult_result_5_v = mmult(mmult_1_v, mmult_3_v);
2577
- assert(mmult_result_5_v.type == ExcelRange);
2578
- assert(mmult_result_5_v.rows == 2);
2579
- assert(mmult_result_5_v.columns == 2);
2580
- ExcelValue *mmult_result_5_a = mmult_result_5_v.array;
2581
- assert(mmult_result_5_a[0].type == ExcelError);
2582
- assert(mmult_result_5_a[1].type == ExcelError);
2583
- assert(mmult_result_5_a[2].type == ExcelError);
2584
- assert(mmult_result_5_a[3].type == ExcelError);
2585
-
2586
- // Test the RANK() function
2587
- ExcelValue rank_1_a[] = { FIVE, BLANK, THREE, ONE, ONE, FOUR, FIVE, TRUE, SIX, new_excel_string("Hi")};
2588
- ExcelValue rank_2_a[] = { FIVE, BLANK, THREE, NA, ONE, FOUR, FIVE, TRUE, SIX, new_excel_string("Hi")};
2589
- ExcelValue rank_1_v = new_excel_range( rank_1_a, 2, 5);
2590
- ExcelValue rank_2_v = new_excel_range( rank_2_a, 2, 5);
2591
-
2592
- // Basics
2593
- assert(rank(THREE, rank_1_v, ZERO).number == 5);
2594
- assert(rank_2(THREE, rank_1_v).number == 5);
2595
- assert(rank(THREE, rank_1_v, ONE).number == 3);
2596
- assert(rank(ONE, rank_1_v, ZERO).number == 6);
2597
- assert(rank(new_excel_string("3"), rank_1_v, ONE).number == 3);
2598
-
2599
- // Errors
2600
- assert(rank(TEN, rank_1_v, ZERO).type == ExcelError);
2601
- assert(rank(THREE, rank_2_v, ZERO).type == ExcelError);
2602
-
2603
-
2604
- // Test the ISNUMBER function
2605
- assert(excel_isnumber(ONE).type == ExcelBoolean);
2606
- assert(excel_isnumber(ONE).number == 1);
2607
- assert(excel_isnumber(BLANK).type == ExcelBoolean);
2608
- assert(excel_isnumber(BLANK).number == 0);
2609
- assert(excel_isnumber(new_excel_string("Hello")).type == ExcelBoolean);
2610
- assert(excel_isnumber(new_excel_string("Hello")).number == 0);
2611
- assert(excel_isnumber(TRUE).type == ExcelBoolean);
2612
- assert(excel_isnumber(TRUE).number == 0);
2613
-
2614
- // Release memory
2615
- free_all_allocated_memory();
2616
-
2617
- return 0;
2618
- }
2619
-
2620
- int main() {
2621
- return test_functions();
2622
- }