/* * Copyright (c) 2026 Rose-Hulman Institute of Technology * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include "practice.h" static void arrays(void) { int a[] = {8, 5, 5, 2}, saved[] = {8, 5, 5, 2}; assert(countDrops(a, 4) == 2); assert(memcmp(a, saved, sizeof a) == 0); assert(countDrops(a, 0) == 0); assert(countDrops(a, 1) == 0); int b[] = {-4, -2, 0, 7}; assert(countDrops(b, 4) == 0); int c[] = {-1, -3, -8}; assert(countDrops(c, 3) == 2); } static void pointers(void) { int n = 14; assert(halveEven(&n) == 1 && n == 7); assert(halveEven(&n) == 0 && n == 7); n = -8; assert(halveEven(&n) == 1 && n == -4); n = -3; assert(halveEven(&n) == 0 && n == -3); n = 0; assert(halveEven(&n) == 1 && n == 0); } static void strings(void) { char text[] = "Hi! Wait..."; assert(countMarks(text) == 4); assert(strcmp(text, "Hi! Wait...") == 0); assert(countMarks("") == 0); assert(countMarks("why?") == 0); assert(countMarks("!.!") == 3); char end[] = {'!', '\0', '.', '\0'}; assert(countMarks(end) == 1); } static void structs(void) { struct stock item = {3, 2}; assert(sellOne(&item) == 1); assert(item.available == 2 && item.sold == 3); item.available = 1; assert(sellOne(&item) == 1); assert(item.available == 0 && item.sold == 4); assert(sellOne(&item) == 0); assert(item.available == 0 && item.sold == 4); } static void heap(void) { int* a = makeRamp(3, 4); assert(a != NULL); assert(a[0] == 4 && a[1] == 5 && a[2] == 6); int* b = makeRamp(1, -2); assert(b != NULL && b[0] == -2); assert(a != b); b[0] = 99; assert(a[0] == 4); free(b); free(a); int* c = makeRamp(1000, -10); assert(c != NULL); for (int i = 0; i < 1000; i++) assert(c[i] == -10 + i); free(c); } int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Choose: arrays pointers strings structs heap\n"); return 2; } if (strcmp(argv[1], "arrays") == 0) arrays(); else if (strcmp(argv[1], "pointers") == 0) pointers(); else if (strcmp(argv[1], "strings") == 0) strings(); else if (strcmp(argv[1], "structs") == 0) structs(); else if (strcmp(argv[1], "heap") == 0) heap(); else { fprintf(stderr, "Unknown exercise: %s\n", argv[1]); return 2; } printf("PASS: %s\n", argv[1]); return 0; }