-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for passing struct parameters by value
- Loading branch information
Showing
67 changed files
with
1,558 additions
and
548 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
...d/parameters/libraries/array_of_structs.c → ...e_parameters/libraries/array_of_structs.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
/* Test that we can pass a pointer to an array of structures as a parameter */ | ||
|
||
#include "array_of_structs.h" | ||
|
||
int validate_struct_array(struct outer *struct_array) { | ||
for (int i = 0; i < 3; i = i + 1) { | ||
if (struct_array[i].a != i * 2) | ||
return i; | ||
return 0; | ||
if (struct_array[i].b.l != i * 3) | ||
return i * 10; | ||
return 0; | ||
if (struct_array[i].b.arr[0] != i * 4) | ||
return i * 25; | ||
return 0; | ||
if (struct_array[i].b.arr[1] != i * 5) | ||
return i * 11; | ||
return 0; | ||
} | ||
return 0; | ||
return 1; // success | ||
} |
2 changes: 2 additions & 0 deletions
2
...d/parameters/libraries/array_of_structs.h → ...e_parameters/libraries/array_of_structs.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/chapter_18/valid/no_structure_parameters/libraries/array_of_structs_client.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Test that we can pass a pointer to an array of structures as a parameter */ | ||
|
||
#include "array_of_structs.h" | ||
|
||
static struct outer static_array[3] = { | ||
{0, {0, {0, 0}}}, {2, {3, {4, 5}}}, {4, {6, {8, 10}}}}; | ||
|
||
int main(void) { | ||
struct outer auto_array[3] = { | ||
{0, {0, {0, 0}}}, {2, {3, {4, 5}}}, {4, {6, {8, 10}}}}; | ||
|
||
// pass pointers to struct arrays with both static and automatic storage | ||
// both have same contents so we can validate them with the same function | ||
|
||
if (!validate_struct_array(static_array)) { | ||
return 1; | ||
} | ||
|
||
if (!validate_struct_array(auto_array)) { | ||
return 2; | ||
} | ||
|
||
return 0; // success | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,5 +87,9 @@ int main(void) { | |
return 3; | ||
} | ||
|
||
if (!test_update_nested_struct_thru_retval()) { | ||
return 4; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
tests/chapter_18/valid/parameters/data_on_page_boundary_linux.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.bss | ||
.align 4096 | ||
.zero 4085 | ||
.bss # .bss is the last segment in the program image - the page after it will be unampped | ||
.align 4096 # force page alignment | ||
.zero 4085 # write a bunch of zeros so on_page_boundary is at the end of the page | ||
.globl on_page_boundary | ||
on_page_boundary: | ||
.zero 9 | ||
.zero 11 | ||
.section ".note.GNU-stack","",@progbits |
8 changes: 4 additions & 4 deletions
8
tests/chapter_18/valid/parameters/data_on_page_boundary_osx.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
.bss | ||
.align 12 | ||
.zero 4085 | ||
.bss # .bss is the last segment in the program image - the page after it will be unampped | ||
.align 12 # force page alignment (2^12 == 4096) | ||
.zero 4085 # write a bunch of zeros so on_page_boundary is at the end of the page | ||
.globl _on_page_boundary | ||
_on_page_boundary: | ||
.zero 9 | ||
.zero 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
tests/chapter_18/valid/parameters/libraries/array_of_structs_client.c
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
tests/chapter_18/valid/parameters/libraries/classify_params.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* Test that we classify structure parameters correctly, | ||
* by passing a variety of structures as arguments. | ||
* Each test function takes only one argument. | ||
* */ | ||
|
||
#include "classify_params.h" | ||
|
||
int test_twelve_bytes(struct twelve_bytes s) { | ||
if (s.i != 0 || strcmp(s.arr, "lmnopqr")) { | ||
return 0; | ||
} | ||
return 1; // success | ||
} | ||
int test_nested_ints(struct nested_ints s) { | ||
if (s.ch1 != 127 || s.nested.i != 2147483647 || s.nested.ch2 != -128) { | ||
return 0; | ||
} | ||
return 1; // success | ||
} | ||
int test_flattened_ints(struct flattened_ints s) { | ||
if (s.c != 127 || s.i != 2147483647 || s.a != -128) { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} | ||
int test_large(struct large s) { | ||
if (s.i != 200000 || s.d != 23.25 || strcmp(s.arr, "abcdefghi")) { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} | ||
int test_two_ints(struct two_ints s) { | ||
if (s.i != 999 || s.i2 != 888) { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} | ||
int test_nested_double(struct nested_double s) { | ||
if (s.array[0] != 25.125e3) { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} | ||
int test_two_eightbytes(struct two_eightbytes s) { | ||
if (s.d != 1000. || s.c != 'x') { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} | ||
int test_pass_in_memory(struct pass_in_memory s) { | ||
if (s.w != 1.7e308 || s.x != -1.7e308 || s.y != -2147483647 || | ||
s.z != -9223372036854775807l) { | ||
return 0; | ||
} | ||
|
||
return 1; // success | ||
} |
78 changes: 78 additions & 0 deletions
78
tests/chapter_18/valid/parameters/libraries/classify_params.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Test that we classify structure parameters correctly, | ||
* by passing a variety of structures as arguments. | ||
* Each test function takes only one argument. | ||
* */ | ||
|
||
#ifdef SUPPRESS_WARNINGS | ||
#ifdef __clang__ | ||
#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" | ||
#else | ||
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" | ||
#endif | ||
#endif | ||
|
||
int strcmp(char *s1, char *s2); | ||
|
||
// from Listing 18-39 | ||
struct twelve_bytes { | ||
int i; | ||
char arr[8]; | ||
}; // two INTEGER eightbytes | ||
|
||
// from Listing 18-40 | ||
struct inner { | ||
int i; | ||
char ch2; | ||
}; | ||
|
||
struct nested_ints { | ||
char ch1; | ||
struct inner nested; | ||
}; // two INTEGER eightbytes | ||
|
||
// from Listing 18-41 | ||
struct flattened_ints { | ||
char c; | ||
int i; | ||
char a; | ||
}; // two INTEGER eightbytes | ||
|
||
// From uncaptioned listing in "Classifying Eightbytes" section | ||
struct large { | ||
int i; | ||
double d; | ||
char arr[10]; | ||
}; // four MEMORY eightbytes | ||
|
||
// Three structure declarations from Listing 18-42 | ||
struct two_ints { | ||
int i; | ||
int i2; | ||
}; // one INTEGER eightbyte | ||
|
||
struct nested_double { | ||
double array[1]; | ||
}; // one SSE eightbyte | ||
|
||
struct two_eightbytes { | ||
double d; | ||
char c; | ||
}; // one SSE eightbyte, one INTEGER eightbyte | ||
|
||
// From Listing 18-47 | ||
struct pass_in_memory { | ||
double w; | ||
double x; | ||
int y; | ||
long z; | ||
}; // four MEMORY eightbytes | ||
|
||
// validation functions defined in library | ||
int test_twelve_bytes(struct twelve_bytes s); | ||
int test_nested_ints(struct nested_ints s); | ||
int test_flattened_ints(struct flattened_ints s); | ||
int test_large(struct large s); | ||
int test_two_ints(struct two_ints s); | ||
int test_nested_double(struct nested_double s); | ||
int test_two_eightbytes(struct two_eightbytes s); | ||
int test_pass_in_memory(struct pass_in_memory s); |
Oops, something went wrong.