File tree 4 files changed +32
-23
lines changed
4 files changed +32
-23
lines changed Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ typedef struct {
11
11
} queue ;
12
12
13
13
int check_input (const int argc , const char * const * const argv );
14
- void read_lines (queue * q );
14
+ int read_lines (queue * const q );
15
15
16
16
// queue.c
17
- queue * new_queue (int max_size );
18
- int enqueue (queue * const q , char * data );
17
+ queue * new_queue (const int max_size );
18
+ int enqueue (queue * const q , const char * const data );
19
19
int dequeue (queue * const q , char * data );
Original file line number Diff line number Diff line change @@ -78,15 +78,18 @@ int check_input(const int argc, const char* const* const argv) {
78
78
}
79
79
80
80
// read_lines(): read input from stdin and enqueue each line
81
- void read_lines (queue * q ) {
81
+ int read_lines (queue * const q ) {
82
82
char temp_line [MAXLEN ];
83
- unsigned long line_len ;
83
+ int line_len ;
84
+ char * line = NULL ;
84
85
85
- while (mygetline (temp_line , MAXLEN ) != 0 ) {
86
- line_len = strlen (temp_line );
87
- char * line = malloc (line_len + 1 );
88
- strncpy (line , temp_line , line_len );
89
- line [line_len ] = '\0' ;
90
- enqueue (q , line );
86
+ while ((line_len = mygetline (temp_line , MAXLEN )) != 0 ) {
87
+ line = malloc ((size_t )line_len + 1 );
88
+ strncpy (line , temp_line , (size_t )(line_len + 1 ));
89
+ if (enqueue (q , line ) == -1 ) {
90
+ free (line );
91
+ return -1 ;
92
+ }
91
93
}
94
+ return 0 ;
92
95
}
Original file line number Diff line number Diff line change 5
5
#include "common.h"
6
6
#include "tail.h"
7
7
8
+ #pragma clang diagnostic ignored "-Wcast-qual"
9
+
8
10
// new_queue(): Initialize a queue structure
9
- queue * new_queue (int max_size ) {
11
+ queue * new_queue (const int max_size ) {
10
12
queue * q = malloc (sizeof (queue ));
11
13
12
14
q -> head = NULL ;
@@ -18,22 +20,25 @@ queue* new_queue(int max_size) {
18
20
}
19
21
20
22
// enqueue(): add an item to the queue
21
- int enqueue (queue * const q , char * data ) {
23
+ int enqueue (queue * const q , const char * const data ) {
22
24
if (q -> current_size == q -> max_size ) {
23
25
// If the queue is full, dequeue the last item to make room
24
26
char * deleted = malloc (MAXLEN );
25
27
dequeue (q , deleted );
28
+ // handle special edgecase for one-node-max queue
29
+ if (q -> max_size == 1 ) {
30
+ q -> head = NULL ;
31
+ }
26
32
free (deleted );
27
33
} else if (q -> current_size > q -> max_size ) {
28
34
printf ("enqueue(): Error - queue exceeds maximum size.\n" );
29
- free (data );
30
35
return -1 ;
31
36
}
32
37
33
38
// Make a new node
34
39
node * new_tail = malloc (sizeof (node ));
35
40
new_tail -> next = NULL ;
36
- new_tail -> data = data ;
41
+ new_tail -> data = ( char * ) data ;
37
42
38
43
// Add the node; handle empty queue if necessary.
39
44
if (q -> head == NULL ) {
Original file line number Diff line number Diff line change @@ -27,11 +27,12 @@ function tail_test(){
27
27
local EXPECTED_OUT_LINES=${2}
28
28
local TEST_NAME=${3}
29
29
30
- ACTUAL_OUT_LINES=$( cat $TESTFILE | bin/tail $N | wc | awk ' {print $1}' )
31
30
32
- if [[ $ACTUAL_OUT_LINES -ne $EXPECTED_OUT_LINES ]]
31
+ ACTUAL_OUT_LINES=$( cat $TESTFILE | valgrind -q --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=42 bin/tail $N 2>&1 | wc -l )
32
+
33
+ if [[ $ACTUAL_OUT_LINES -ne $EXPECTED_OUT_LINES || $? -eq 42 ]]
33
34
then
34
- fatal " Test failure, $TEST_NAME : $ACTUAL_OUT_LINES != $EXPECTED_OUT_LINES "
35
+ fatal " Test failure, $TEST_NAME : $ACTUAL_OUT_LINES != $EXPECTED_OUT_LINES (check Valgrind output) "
35
36
fi
36
37
}
37
38
@@ -48,10 +49,10 @@ tail_test 25 20 "larger n"
48
49
tail_test 1 1 " n = 1"
49
50
tail_test 0 0 " n = 0"
50
51
51
- clean_up
52
- echo " Generating huge input for tail; this is slow."
53
- generate_test_file 20000
54
- echo " Huge input generated."
55
- tail_test " " 10 " Huge input"
52
+ # clean_up
53
+ # echo "Generating huge input for tail; this is slow."
54
+ # generate_test_file 20000
55
+ # echo "Huge input generated."
56
+ # tail_test "" 10 "Huge input"
56
57
57
58
echo " 5.13: PASS!"
You can’t perform that action at this time.
0 commit comments