File size: 2,258 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>

void setUp(void) {
  /* Setup code here, or leave empty */
}

void tearDown(void) {
  /* Cleanup code here, or leave empty */
}

/* Test that both uppercase 'K' and lowercase 'k' map to power 1. */
void test_suffix_power_k_variants(void) {
  TEST_ASSERT_EQUAL_INT(1, suffix_power('K'));
  TEST_ASSERT_EQUAL_INT(1, suffix_power('k'));
}

/* Test the full set of supported uppercase suffixes map to expected powers. */
void test_suffix_power_all_uppercase(void) {
  struct {
    char suf;
    int expected;
  } cases[] = {
    { 'M', 2 },
    { 'G', 3 },
    { 'T', 4 },
    { 'P', 5 },
    { 'E', 6 },
    { 'Z', 7 },
    { 'Y', 8 },
    { 'R', 9 },
    { 'Q', 10 },
  };

  for (size_t i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
    TEST_ASSERT_EQUAL_INT(cases[i].expected, suffix_power(cases[i].suf));
  }
}

/* Lowercase variants other than 'k' should not be recognized and return 0. */
void test_suffix_power_lowercase_unrecognized(void) {
  char lows[] = { 'm', 'g', 't', 'p', 'e', 'z', 'y', 'r', 'q' };
  for (size_t i = 0; i < sizeof(lows)/sizeof(lows[0]); i++) {
    TEST_ASSERT_EQUAL_INT(0, suffix_power(lows[i]));
  }
}

/* Invalid characters should return 0. */
void test_suffix_power_invalid_chars(void) {
  TEST_ASSERT_EQUAL_INT(0, suffix_power('A'));
  TEST_ASSERT_EQUAL_INT(0, suffix_power('b'));
  TEST_ASSERT_EQUAL_INT(0, suffix_power('0'));
  TEST_ASSERT_EQUAL_INT(0, suffix_power(' '));
  TEST_ASSERT_EQUAL_INT(0, suffix_power('\0'));
  TEST_ASSERT_EQUAL_INT(0, suffix_power('\n'));
}

/* Boundary check: just below/above valid range should return 0. */
void test_suffix_power_boundary_non_suffixes(void) {
  TEST_ASSERT_EQUAL_INT(0, suffix_power('@')); /* before 'A' */
  TEST_ASSERT_EQUAL_INT(0, suffix_power('[')); /* after 'Z' */
  TEST_ASSERT_EQUAL_INT(0, suffix_power('`')); /* before 'a' */
  TEST_ASSERT_EQUAL_INT(0, suffix_power('{')); /* after 'z' */
}

int main(void) {
  UNITY_BEGIN();

  RUN_TEST(test_suffix_power_k_variants);
  RUN_TEST(test_suffix_power_all_uppercase);
  RUN_TEST(test_suffix_power_lowercase_unrecognized);
  RUN_TEST(test_suffix_power_invalid_chars);
  RUN_TEST(test_suffix_power_boundary_non_suffixes);

  return UNITY_END();
}