File size: 3,335 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>

/* Unity required functions */
void setUp(void) {
  /* No setup needed for these tests */
}

void tearDown(void) {
  /* No teardown needed for these tests */
}

/*
  Target under test (declared static in dd.c). Since this test file is
  #included into dd.c, we can call it directly.
  static int parse_symbols (char const *str, struct symbol_value const *table,
                           bool exclusive, char const *error_msgid);

  We also use the static symbol tables and constants from dd.c:
  - conversions[] with C_* constants
  - statuses[] with STATUS_* constants
  - flags[] with O_* constants
*/

/* Basic: single symbol from conversions table */
void test_parse_symbols_single_swab(void) {
  int val = parse_symbols("swab", conversions, false, "invalid conversion");
  /* swab has C_SWAB | C_TWOBUFS */
  TEST_ASSERT_EQUAL_INT(C_SWAB | C_TWOBUFS, val);
}

/* Multiple symbols OR-combination from conversions table */
void test_parse_symbols_multiple_or(void) {
  int val = parse_symbols("swab,sync", conversions, false, "invalid conversion");
  int expected = (C_SWAB | C_TWOBUFS) | C_SYNC;
  TEST_ASSERT_EQUAL_INT(expected, val);
}

/* Duplicate tokens should not change the result beyond the bit once */
void test_parse_symbols_duplicate_tokens(void) {
  int val = parse_symbols("ucase,ucase", conversions, false, "invalid conversion");
  int expected = C_UCASE | C_TWOBUFS;
  TEST_ASSERT_EQUAL_INT(expected, val);
}

/* Exclusive mode: last value wins (statuses table) */
void test_parse_symbols_exclusive_last_wins_progress(void) {
  int val = parse_symbols("none,progress", statuses, true, "invalid status");
  TEST_ASSERT_EQUAL_INT(STATUS_PROGRESS, val);
}

void test_parse_symbols_exclusive_last_wins_none(void) {
  int val = parse_symbols("progress,none", statuses, true, "invalid status");
  TEST_ASSERT_EQUAL_INT(STATUS_NONE, val);
}

/* Mixed conversions: ensure expected OR of values including C_TWOBUFS
   for entries that specify it and not for those that don't. */
void test_parse_symbols_ascii_ucase_sparse(void) {
  int val = parse_symbols("ascii,ucase,sparse", conversions, false, "invalid conversion");
  int expected = 0;
  expected |= C_ASCII;        /* from ascii */
  expected |= C_UNBLOCK;      /* from ascii */
  expected |= C_TWOBUFS;      /* ascii has C_TWOBUFS */
  expected |= C_UCASE;        /* ucase */
  expected |= C_TWOBUFS;      /* ucase also has C_TWOBUFS (already set) */
  expected |= C_SPARSE;       /* sparse has no C_TWOBUFS */
  TEST_ASSERT_EQUAL_INT(expected, val);
}

/* Use the flags table to ensure O_* flags combine as expected */
void test_parse_symbols_flags_combination(void) {
  int val = parse_symbols("skip_bytes,seek_bytes", flags, false, "invalid flag");
  int expected = O_SKIP_BYTES | O_SEEK_BYTES;
  TEST_ASSERT_EQUAL_INT(expected, val);
}

int main(void) {
  UNITY_BEGIN();
  RUN_TEST(test_parse_symbols_single_swab);
  RUN_TEST(test_parse_symbols_multiple_or);
  RUN_TEST(test_parse_symbols_duplicate_tokens);
  RUN_TEST(test_parse_symbols_exclusive_last_wins_progress);
  RUN_TEST(test_parse_symbols_exclusive_last_wins_none);
  RUN_TEST(test_parse_symbols_ascii_ucase_sparse);
  RUN_TEST(test_parse_symbols_flags_combination);
  return UNITY_END();
}