File: test_util_getlong.c

package info (click to toggle)
portsentry 2.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: ansic: 6,473; sh: 916; perl: 18; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 1,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "../src/util.h"
#include "../src/portsentry.h"

void TestGetLongValidNumbers(void);
void TestGetLongNullInput(void);
void TestGetLongInvalidInput(void);
void TestGetLongBoundaryCases(void);

void TestGetLongValidNumbers(void) {
  assert(GetLong("123") == 123);
  assert(GetLong("0") == 0);
  assert(GetLong("-456") == -456);
  printf("Valid numbers test passed\n");
}

void TestGetLongNullInput(void) {
  assert(GetLong(NULL) == ERROR);
  printf("Null input test passed\n");
}

void TestGetLongInvalidInput(void) {
  assert(GetLong("abc") == ERROR);
  assert(GetLong("") == ERROR);
  assert(GetLong(" ") == ERROR);
  assert(GetLong("123abc") == ERROR);  // trailing characters
  printf("Invalid input test passed\n");
}

void TestGetLongBoundaryCases(void) {
  char max_buffer[32];
  char min_buffer[32];
  snprintf(max_buffer, sizeof(max_buffer), "%ld", LONG_MAX);
  snprintf(min_buffer, sizeof(min_buffer), "%ld", LONG_MIN);

  assert(GetLong(max_buffer) == ERROR);
  assert(GetLong(min_buffer) == ERROR);
  printf("Boundary cases test passed\n");
}

int main(void) {
  TestGetLongValidNumbers();
  TestGetLongNullInput();
  TestGetLongInvalidInput();
  TestGetLongBoundaryCases();
  printf("All GetLong tests passed!\n");
  return 0;
}