File size: 3,877 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>

/* The test file is included into the dd.c translation unit, so the following
   globals and functions are visible here:
   - static globals: obuf, ibuf, conversions_mask, page_size,
     input_blocksize, output_blocksize
   - constants/masks: C_TWOBUFS
   - function under test: alloc_obuf(void)
   - helper used by alloc_obuf when not C_TWOBUFS: alloc_ibuf(void)
*/

void setUp(void) {
    /* Provide a clean baseline for each test. We do not free previously
       allocated buffers to avoid coupling to allocator specifics; instead we
       reset pointers to ensure alloc_obuf's behavior is testable in isolation. */
    obuf = NULL;
    ibuf = NULL;
    conversions_mask = 0;
    /* Reasonable sizes to ensure allocations succeed. */
    page_size = 4096;        /* typical page size and valid alignment */
    input_blocksize = 64;    /* >0 so alloc_ibuf can allocate */
    output_blocksize = 128;  /* >0 so alloc_obuf(C_TWOBUFS) can allocate */
}

void tearDown(void) {
    /* Nothing to clean beyond pointer resets done in setUp. */
}

/* Test: With C_TWOBUFS set and obuf == NULL, alloc_obuf allocates a distinct
   output buffer and does not touch ibuf. */
void test_alloc_obuf_twobufs_allocates_separate_buffer(void) {
    conversions_mask = C_TWOBUFS;
    /* Ensure a valid environment for allocation. */
    page_size = 4096;
    output_blocksize = 128;
    ibuf = NULL;
    obuf = NULL;

    alloc_obuf();

    TEST_ASSERT_NOT_NULL(obuf);
    TEST_ASSERT_NULL(ibuf); /* alloc_obuf should not allocate ibuf in this path */
}

/* Test: Without C_TWOBUFS, alloc_obuf uses alloc_ibuf and sets obuf == ibuf. */
void test_alloc_obuf_without_twobufs_uses_ibuf_buffer(void) {
    conversions_mask = 0; /* no C_TWOBUFS */
    page_size = 4096;
    input_blocksize = 64;  /* ensure alloc_ibuf can allocate */
    obuf = NULL;
    ibuf = NULL;

    alloc_obuf();

    TEST_ASSERT_NOT_NULL(ibuf);
    TEST_ASSERT_EQUAL_PTR(ibuf, obuf);
}

/* Test: If obuf is already set, alloc_obuf is idempotent and returns early
   without changing obuf or ibuf regardless of flags. */
void test_alloc_obuf_idempotent_when_already_allocated(void) {
    static char dummy_out[8];
    /* Pre-set obuf to a known pointer. */
    obuf = dummy_out;
    /* Set flags such that alloc_obuf would otherwise allocate or assign. */
    conversions_mask = C_TWOBUFS;
    output_blocksize = 256;
    ibuf = NULL;

    alloc_obuf();

    TEST_ASSERT_EQUAL_PTR(dummy_out, obuf);
    TEST_ASSERT_NULL(ibuf);
}

/* Test: With C_TWOBUFS set and a preexisting ibuf, alloc_obuf must not modify
   ibuf and must allocate a distinct obuf. */
void test_alloc_obuf_twobufs_does_not_touch_existing_ibuf(void) {
    static char existing_ibuf[16];
    ibuf = existing_ibuf;
    obuf = NULL;
    conversions_mask = C_TWOBUFS;
    output_blocksize = 128;

    alloc_obuf();

    TEST_ASSERT_NOT_NULL(obuf);
    TEST_ASSERT_EQUAL_PTR(existing_ibuf, ibuf);
    TEST_ASSERT_NOT_EQUAL(obuf, ibuf);
}

/* Test: Without C_TWOBUFS and a preexisting ibuf, alloc_obuf should not
   allocate but simply set obuf to the existing ibuf. */
void test_alloc_obuf_non_twobufs_uses_existing_ibuf(void) {
    static char existing_ibuf[32];
    ibuf = existing_ibuf;
    obuf = NULL;
    conversions_mask = 0; /* no C_TWOBUFS */

    alloc_obuf();

    TEST_ASSERT_EQUAL_PTR(existing_ibuf, ibuf);
    TEST_ASSERT_EQUAL_PTR(ibuf, obuf);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_alloc_obuf_twobufs_allocates_separate_buffer);
    RUN_TEST(test_alloc_obuf_without_twobufs_uses_ibuf_buffer);
    RUN_TEST(test_alloc_obuf_idempotent_when_already_allocated);
    RUN_TEST(test_alloc_obuf_twobufs_does_not_touch_existing_ibuf);
    RUN_TEST(test_alloc_obuf_non_twobufs_uses_existing_ibuf);
    return UNITY_END();
}