Branch data MC/DC data Line data Source code
1 : : : #include "unity.h"
2 : : : #include "file_reader.h"
3 : : : #include <sys/stat.h>
4 : : : #include <stdlib.h>
5 : : : #include <stdio.h>
6 : : : #include <stdbool.h>
7 : : : #include <string.h>
8 : : : #include <setjmp.h>
9 : : :
10 : : :
11 : : : static bool wrap_fopen_fail = false;
12 : : : static bool wrap_perror_called = false;
13 : : : static bool wrap_exit_called = false;
14 : : : static int wrap_exit_status = 0;
15 : : :
16 : : : FILE *test_file;
17 : : : jmp_buf exit_env;
18 : : :
19 : : : // ---- WRAPS ------
20 : : :
21 : : : /** @brief fopen function mock */
22 : : : FILE *__real_fopen(const char *path, const char *mode);
23 : : 4 : FILE *__wrap_fopen(const char *path, const char *mode)
24 : : : {
25 [ + + ]: [ T F ]: 4 : if(wrap_fopen_fail)
26 : : 1 : return NULL;
27 : : 3 : return __real_fopen(path, mode);
28 : : : }
29 : : :
30 : : : /** @brief perror function mock */
31 : : : void __real_perror(const char *s);
32 : : 1 : void __wrap_perror(const char *s)
33 : : : {
34 : : 1 : wrap_perror_called = true;
35 : : 1 : __real_perror(s);
36 : : 1 : }
37 : : :
38 : : : /** @brief exit function mock */
39 : : 1 : void __wrap_exit(int status)
40 : : : {
41 : : 1 : wrap_exit_called = true;
42 : : 1 : wrap_exit_status = status;
43 : : 1 : longjmp(exit_env, 1);
44 : : : }
45 : : :
46 : : : /**
47 : : : * @brief setUp function
48 : : : */
49 : : 4 : void setUp()
50 : : : {
51 : : 4 : wrap_fopen_fail = false;
52 : : 4 : wrap_perror_called = false;
53 : : 4 : wrap_exit_called = false;
54 : : 4 : wrap_exit_status = 0;
55 : : 4 : }
56 : : :
57 : : : /**
58 : : : * @brief tearDown function
59 : : : */
60 : : 4 : void tearDown()
61 : : : {
62 [ + + ]: [ T F ]: 4 : if (test_file)
63 : : : {
64 : : 3 : fclose(test_file);
65 : : 3 : test_file = NULL;
66 : : : }
67 : : :
68 : : 4 : }
69 : : :
70 : : : /**
71 : : : * @test
72 : : : * @brief Test open_file() when fopen fails
73 : : : * \anchor test_open_file_fopen_fail_should_exit
74 : : : * test ID [TC_FILE_READER_001](@ref TC_FILE_READER_001)
75 : : : */
76 : : 1 : void test_open_file_fopen_fail_should_exit()
77 : : : {
78 : : 1 : wrap_fopen_fail = true;
79 : : :
80 : : : // Test Case ID: TC_FILE_READER_001
81 : : :
82 [ + + ]: [ T F ]: 1 : if (setjmp(exit_env) == 0) {
83 : : 1 : open_file("invalid/path.txt"); // Vai cair no longjmp da wrap_exit
84 : : 0 : TEST_FAIL_MESSAGE("exit() was not called as expected");
85 : : : }
86 : : :
87 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(wrap_perror_called);
88 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(wrap_exit_called);
89 : : 1 : TEST_ASSERT_EQUAL(EXIT_FAILURE, wrap_exit_status);
90 : : 1 : }
91 : : :
92 : : : /**
93 : : : * @test
94 : : : * @brief Test for the function open_file on file_reader.c [SwR-9] (@ref SwR-9), [SwR-11] (@ref SwR-11)
95 : : : * \anchor test_open_file_not_null_and_skip_header
96 : : : * test ID [TC_FILE_READER_002](@ref TC_FILE_READER_002)
97 : : : */
98 : : 1 : void test_open_file_not_null_and_skip_header()
99 : : : {
100 : : 1 : const char *test_filename = "tcs/cenario.txt";
101 : : 1 : test_file = open_file(test_filename);
102 : : :
103 : : : // Test Case ID: TC_FILE_READER_002
104 : : :
105 : : : // Checks if test_file pointer is not null
106 [ - + ]: [ T f ]: 1 : TEST_ASSERT_NOT_NULL(test_file);
107 : : :
108 : : : char buffer[100];
109 : : 1 : fgets(buffer, sizeof(buffer), test_file);
110 : : :
111 : : : /* if buffer is the same as the second line of the file,
112 : : : it means test_file points to the correct line, skipping
113 : : : the header
114 : : : */
115 : : 1 : TEST_ASSERT_EQUAL_STRING("60 1 108 0 1 1 0 0\n", buffer);
116 : : :
117 : : 1 : }
118 : : :
119 : : : /**
120 : : : * @test
121 : : : * @brief Tests for the function read_sensor_data on file_reader.c [SwR-9] (@ref SwR-9), [SwR-11] (@ref SwR-11)
122 : : : * \anchor test_read_sensor_data_valid_data
123 : : : * test ID [TC_FILE_READER_003](@ref TC_FILE_READER_003)
124 : : : */
125 : : 1 : void test_read_sensor_data_valid_data()
126 : : : {
127 : : : sensors_input_data test_sensor_data;
128 : : 1 : const char *test_filename = "tcs/cenario.txt";
129 : : 1 : test_file = open_file(test_filename);
130 : : :
131 : : : // Test Case ID: TC_FILE_READER_003
132 : : : /* Checks if the function successfully reads
133 : : : all the data from second line
134 : : : */
135 : : 1 : TEST_ASSERT_EQUAL(1, read_sensor_data(test_file, &test_sensor_data));
136 : : :
137 : : : // Checks if values match expected ones from second line
138 : : 1 : TEST_ASSERT_EQUAL_FLOAT(60.0, test_sensor_data.obstacle_distance);
139 : : 1 : TEST_ASSERT_EQUAL(1, test_sensor_data.has_obstacle);
140 : : 1 : TEST_ASSERT_EQUAL_FLOAT(108.0, test_sensor_data.relative_velocity);
141 : : 1 : TEST_ASSERT_EQUAL(0, test_sensor_data.brake_pedal);
142 : : 1 : TEST_ASSERT_EQUAL(1, test_sensor_data.accelerator_pedal);
143 : : 1 : TEST_ASSERT_EQUAL(1, test_sensor_data.aeb_system_enabled);
144 : : 1 : TEST_ASSERT_EQUAL(0, test_sensor_data.reverse_enabled);
145 : : 1 : TEST_ASSERT_EQUAL_FLOAT(0.0,test_sensor_data.relative_acceleration);
146 : : :
147 : : 1 : }
148 : : :
149 : : : /** @test
150 : : : * \anchor test_read_sensor_data_eof
151 : : : * test ID [TC_FILE_READER_004](@ref TC_FILE_READER_004)
152 : : : */
153 : : 1 : void test_read_sensor_data_eof()
154 : : : {
155 : : : sensors_input_data test_sensor_data;
156 : : 1 : const char *test_filename = "tcs/cenario.txt";
157 : : 1 : test_file = open_file(test_filename);
158 : : :
159 : : : // Test Case ID: TC_FILE_READER_004
160 [ + + ]: [ T F ]: 6 : while (read_sensor_data(test_file, &test_sensor_data) == 1);
161 : : :
162 : : 1 : TEST_ASSERT_EQUAL(0, read_sensor_data(test_file, &test_sensor_data));
163 : : 1 : }
164 : : :
165 : : :
166 : : :
167 : : :
168 : : 1 : int main()
169 : : : {
170 : : 1 : UNITY_BEGIN();
171 : : 1 : RUN_TEST(test_open_file_fopen_fail_should_exit);
172 : : 1 : RUN_TEST(test_open_file_not_null_and_skip_header);
173 : : 1 : RUN_TEST(test_read_sensor_data_valid_data);
174 : : 1 : RUN_TEST(test_read_sensor_data_eof);
175 : : 1 : return UNITY_END();
176 : : :
177 : : : }
|