Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool test_list_map()
- {
- // Nested function, available in GCC.
- int square(int n)
- {
- return n * n;
- }
- bool failed = false;
- // List p: 1 -> 2 -> 3 -> NULL
- List *lp = list_init(3, 1, 2, 3);
- if (lp == NULL) {
- perror("Failed to allocate List lp");
- return false;
- }
- // List q: 1 -> 4 -> 9 -> NULL
- // Traverse in functional style.
- List *lq = list_map(lp, square);
- if (lq == NULL) {
- perror("Failed to allocate List lq");
- failed = true;
- goto LIST_P_FREE;
- }
- int arr[] = {1, 4, 9};
- size_t i = 0;
- for (Node *it = list_start(lq); !list_end(it); it = list_next(it)) {
- if (node_value(it) != arr[i]) {
- failed = true;
- goto LIST_Q_FREE;
- }
- i++;
- }
- LIST_Q_FREE:
- list_free(lq);
- LIST_P_FREE:
- list_free(lp);
- if (failed) {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement