# assert

***

**1. Verifying Function Arguments:**

```c
void validate_args(int num1, int num2) {
  assert(num1 >= 0 && num2 >= 0);
}
```

**2. Checking Array Bounds:**

```c
int arr[] = {1, 2, 3};
int index = 1;
assert(index >= 0 && index < sizeof(arr) / sizeof(arr[0]));
```

**3. Ensuring Resource Allocation:**

```c
FILE *file = fopen("myfile.txt", "r");
assert(file != NULL);
```

**4. Verifying Loop Termination:**

```c
while (condition) {
  // Loop body
  assert(condition == true); // Ensure loop termination condition
}
```

**5. Testing Function Return Values:**

```c
int divide(int num1, int num2) {
  assert(num2 != 0);
  return num1 / num2;
}
```

**6. Verifying Object States:**

```c
struct Point {
  int x;
  int y;
};

struct Point point;
point.x = 5;
point.y = 10;
assert(point.x == 5 && point.y == 10);
```

**7. Ensuring Function Preconditions:**

```c
void calculate_area(float length, float width) {
  assert(length > 0 && width > 0);
  // Calculate area based on preconditions
}
```

**8. Verifying File Access:**

```c
FILE *file = fopen("myfile.txt", "w");
assert(file != NULL);
```

**9. Testing Pointer Validity:**

```c
int *ptr = malloc(sizeof(int));
assert(ptr != NULL);
```

**10. Ensuring Data Consistency:**

```c
struct Employee {
  char *name;
  int age;
};

struct Employee employee;
employee.name = "John";
employee.age = 30;
assert(strcmp(employee.name, "John") == 0 && employee.age == 30);
```

**11. Verifying Memory Allocations:**

```c
int *ptr = calloc(10, sizeof(int));
assert(ptr != NULL);
```

**12. Debugging Pointer Errors:**

```c
int *ptr;
assert(ptr != NULL); // Assert that the pointer has been initialized
```

**13. Ensuring File Closure:**

```c
FILE *file = fopen("myfile.txt", "r");
assert(file != NULL);
fclose(file);
assert(file == NULL);
```

**14. Verifying Data Structures:**

```c
struct Node {
  int data;
  struct Node *next;
};

struct Node *head = NULL;
head = insert_node(head, 5);
assert(head != NULL);
```

**15. Testing Function Parameters:**

```c
int max(int num1, int num2) {
  assert(num1 >= 0 && num2 >= 0);
  return (num1 > num2) ? num1 : num2;
}
```

**16. Debugging Array Indexing Errors:**

```c
int arr[] = {1, 2, 3};
assert(arr[index] >= 0 && arr[index] < sizeof(arr) / sizeof(arr[0]));
```

**17. Verifying Function Behavior:**

```c
int add(int a, int b) {
  assert(a >= 0 && b >= 0);
  return a + b;
}
```

**18. Testing Dynamic Memory Allocations:**

```c
int *ptr = (int *)malloc(sizeof(int));
assert(ptr != NULL);
```

**19. Ensuring Object Initialization:**

```c
struct Person {
  char *name;
  int age;
};

struct Person person = { .name = "John", .age = 30 };
assert(person.name != NULL && person.age == 30);
```

**20. Verifying String Operations:**

```c
char *str = "Hello";
assert(strcmp(str, "Hello") == 0);
```

**21. Testing Pointer Dereferencing:**

```c
int *ptr = malloc(sizeof(int));
*ptr = 5;
assert(*ptr == 5);
```

**22. Ensuring Resource Release:**

```c
FILE *file = fopen("myfile.txt", "w");
assert(file != NULL);
fclose(file);
```

**23. Verifying Conditional Statements:**

```c
if (condition) {
  assert(condition == true); // Ensure condition is true
}
```

**24. Testing Loop Invariants:**

```c
for (int i = 0; i < 10; i++) {
  assert(i >= 0 && i < 10); // Ensure loop invariant
}
```

**25. Verifying Function Call Stack:**

```c
void func1();
void func2();

void func1() {
  func2();
  assert(__builtin_return_address(0) == &func2);
}
```

**26. Debugging Infinite Loops:**

```c
while (condition) {
  // Loop body
  assert(condition == false); // Ensure loop termination condition
}
```

**27. Testing Function Exit Conditions:**

```c
void exit_function() {
  assert(__builtin_return_address(0) == &exit_function);
}
```

**28. Verifying Function Side Effects:**

```c
void increment_global() {
  static int global_var = 0;
  global_var++;

  assert(global_var == 1); // Ensure side effect on global variable
}
```

**29. Testing Dynamic Array Allocations:**

```c
int *arr = (int *)malloc(5 * sizeof(int));
assert(arr != NULL && sizeof(arr) / sizeof(arr[0]) == 5);
```

**30. Verifying Function Pointers:**

```c
int add(int a, int b) { return a + b; }
int *fptr = &add;

assert(*fptr(5, 10) == 15);
```

**31. Debugging Semaphore Operations:**

```c
sem_t semaphore;

void init_semaphore() {
  assert(sem_init(&semaphore, 0, 1) == 0);
}
```

**32. Verifying Thread States:**

```c
pthread_t thread;

void thread_routine() {
  assert(pthread_self() == thread);
}
```

**33. Testing Signal Handling:**

```c
void signal_handler() {
  assert(true); // Ensure signal handler is called
}
```

**34. Verifying Memory Boundaries:**

```c
char *buf = (char *)malloc(100);
assert(buf < buf + 100);
```

**35. Debugging Pointer Arithmetic:**

```c
int *ptr = malloc(sizeof(int));
ptr++;

assert(ptr - 1 == buf);
```

**36. Testing Function Return Codes:**

```c
FILE *file = fopen("myfile.txt", "r");
assert(file != NULL || errno == ENOENT);
```

**37. Verifying File Permission Flags:**

```c
FILE *file = fopen("myfile.txt", "r+");
assert(file != NULL && ftell(file) == 0);
```

**38. Debugging Floating-Point Errors:**

```c
float f = 0.1;
assert(fabsf(f - 0.1) < FLT_EPSILON);
```

**39. Verifying Pointer Conversions:**

```c
struct Node *nptr = malloc(sizeof(struct Node));
struct Node *nptr2 = (struct Node *)nptr;

assert(nptr == nptr2);
```

**40. Testing Bitwise Operations:**

```c
int a = 5;
assert((a & 1) == 1);
```

**41. Verifying Macro Expansions:**

```c
#define MAX_SIZE 100
assert(MAX_SIZE == 100);
```

**42. Debugging Preprocessor Conditional Compilation:**

```c
#ifdef DEBUG
  printf("Debug message"); // Only print when DEBUG is defined
#endif

assert(defined(DEBUG) || !defined(DEBUG));
```

**43. Verifying Inline Functions:**

```c
inline int square(int x) { return x * x; }

assert(square(5) == 25);
```

**44. Testing Type Casting:**

```c
int a = 5;
double b = (double)a;

assert(b == 5.0);
```

**45. Verifying Union Types:**

```c
union MyUnion {
  int a;
  char b;
};

union MyUnion u;
u.a = 5;

assert(u.b == 0);
```

**46. Debugging Structure Initialization:**

```c
struct MyStruct {
  int a;
  char b;
};

struct MyStruct s = { .a = 5, .b = 'x' };

assert(s.a == 5 && s.b == 'x');
```

**47. Verifying Enum Types:**

```c
enum MyEnum {
  A,
  B,
  C
};

enum MyEnum e = A;

assert(e == A);
```

**48. Testing Bitfields:**

```c
struct Bitfield {
  unsigned int a : 3;
  unsigned int b : 4;
};

struct Bitfield bf = { .a = 1, .b = 5 };

assert(bf.a == 1 && bf.b == 5);
```

**49. Verifying C Macros:**

```c
#define MACRO(x) x * x

assert(MACRO(5) == 25);
```

**50. Debugging Stack Overflow:**

```c
int recursion(int n) {
  if (n == 0)
    return 1;

  assert(n > 0);
  return n * recursion(n - 1);
}
```
