Skip to content

Commit e04991c

Browse files
author
Joshua Goller
committed
tail builds / tests correctly
1 parent 376ad91 commit e04991c

File tree

5 files changed

+75
-71
lines changed

5 files changed

+75
-71
lines changed

ch-5/Makefile

+2-6
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ ifdef RUN_TESTS
4141
endif
4242

4343
# tabber covers the entab / detab exercises in 5.11 and 5.12
44-
tabber: clean
44+
# tail is exercise 5.13
45+
tabber tail: clean
4546
$(COMPILE) -I ch-5/$@/include ch-5/$@/src/*.c -o bin/$@
4647
./ch-5/$@/$@-test.sh
4748

48-
# tail is exercise 5.13; I decided external tests were better than unit tests.
49-
tail: clean
50-
$(COMPILE) -I ch-5/tail/include/ ch-5/tail/src/5.13.c -o bin/tail
51-
./tail/tail_test.sh
52-
5349
# sort is exercises 5.14 through 5.17
5450
sort: clean
5551
$(COMPILE) -I ch-5/sort/include/ ch-5/sort/src/{compare,input,main,sort,strings}.c -o bin/$@

ch-5/tail/include/5.13.h ch-5/tail/include/tail.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ typedef struct {
1111
} queue;
1212

1313
int check_input(const int argc, const char* const* const argv);
14+
void read_lines(queue* q);
1415

16+
// queue.c
1517
queue* new_queue(int max_size);
16-
void read_lines(queue* q);
1718
int enqueue(queue* const q, char* data);
1819
int dequeue(queue* const q, char* data);

ch-5/tail/src/5.13.c ch-5/tail/src/main.c

+2-62
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include "5.13.h"
21
#include <ctype.h>
32
#include <stdio.h>
43
#include <stdlib.h>
54
#include <string.h>
5+
66
#include "common.h"
7+
#include "tail.h"
78

89
/*
910
* Exercise 5-13. Write the program tail, which prints the last n lines of its
@@ -89,64 +90,3 @@ void read_lines(queue* q) {
8990
enqueue(q, line);
9091
}
9192
}
92-
93-
// new_queue(): Initialize a queue structure
94-
queue* new_queue(int max_size) {
95-
queue* q = malloc(sizeof(queue));
96-
97-
q->head = NULL;
98-
q->tail = NULL;
99-
q->max_size = max_size;
100-
q->current_size = 0;
101-
102-
return q;
103-
}
104-
105-
// enqueue(): add an item to the queue
106-
int enqueue(queue* const q, char* data) {
107-
if (q->current_size == q->max_size) {
108-
// If the queue is full, dequeue the last item to make room
109-
char* deleted = malloc(MAXLEN);
110-
dequeue(q, deleted);
111-
free(deleted);
112-
} else if (q->current_size > q->max_size) {
113-
printf("enqueue(): Error - queue exceeds maximum size.\n");
114-
free(data);
115-
return -1;
116-
}
117-
118-
// Make a new node
119-
node* new_tail = malloc(sizeof(node));
120-
new_tail->next = NULL;
121-
new_tail->data = data;
122-
123-
// Add the node; handle empty queue if necessary.
124-
if (q->head == NULL) {
125-
q->head = new_tail;
126-
q->tail = new_tail;
127-
}
128-
q->tail->next = new_tail;
129-
q->tail = q->tail->next;
130-
q->current_size++;
131-
132-
return 0;
133-
}
134-
135-
// dequeue(): remove an item from the queue
136-
int dequeue(queue* const q, char* const data) {
137-
if (!(q->current_size && q->head)) {
138-
// Queue is empty
139-
*data = '\0';
140-
return 0;
141-
} else {
142-
// Remove current head; make the next item the new head
143-
node* old_head = q->head;
144-
strcpy(data, old_head->data);
145-
q->head = old_head->next;
146-
147-
free(old_head->data);
148-
free(old_head);
149-
q->current_size--;
150-
return 1;
151-
}
152-
}

ch-5/tail/src/queue.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
#include "common.h"
6+
#include "tail.h"
7+
8+
// new_queue(): Initialize a queue structure
9+
queue* new_queue(int max_size) {
10+
queue* q = malloc(sizeof(queue));
11+
12+
q->head = NULL;
13+
q->tail = NULL;
14+
q->max_size = max_size;
15+
q->current_size = 0;
16+
17+
return q;
18+
}
19+
20+
// enqueue(): add an item to the queue
21+
int enqueue(queue* const q, char* data) {
22+
if (q->current_size == q->max_size) {
23+
// If the queue is full, dequeue the last item to make room
24+
char* deleted = malloc(MAXLEN);
25+
dequeue(q, deleted);
26+
free(deleted);
27+
} else if (q->current_size > q->max_size) {
28+
printf("enqueue(): Error - queue exceeds maximum size.\n");
29+
free(data);
30+
return -1;
31+
}
32+
33+
// Make a new node
34+
node* new_tail = malloc(sizeof(node));
35+
new_tail->next = NULL;
36+
new_tail->data = data;
37+
38+
// Add the node; handle empty queue if necessary.
39+
if (q->head == NULL) {
40+
q->head = new_tail;
41+
q->tail = new_tail;
42+
}
43+
q->tail->next = new_tail;
44+
q->tail = q->tail->next;
45+
q->current_size++;
46+
47+
return 0;
48+
}
49+
50+
// dequeue(): remove an item from the queue
51+
int dequeue(queue* const q, char* const data) {
52+
if (!(q->current_size && q->head)) {
53+
// Queue is empty
54+
*data = '\0';
55+
return 0;
56+
} else {
57+
// Remove current head; make the next item the new head
58+
node* old_head = q->head;
59+
strcpy(data, old_head->data);
60+
q->head = old_head->next;
61+
62+
free(old_head->data);
63+
free(old_head);
64+
q->current_size--;
65+
return 1;
66+
}
67+
}

ch-5/tail/tail_test.sh ch-5/tail/tail-test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function tail_test(){
2828
local TEST_NAME=${3}
2929

3030
ACTUAL_OUT_LINES=$(cat $TESTFILE | bin/tail $N | wc | awk '{print $1}')
31-
31+
3232
if [[ $ACTUAL_OUT_LINES -ne $EXPECTED_OUT_LINES ]]
3333
then
3434
fatal "Test failure, $TEST_NAME: $ACTUAL_OUT_LINES != $EXPECTED_OUT_LINES"
@@ -50,7 +50,7 @@ tail_test 0 0 "n = 0"
5050

5151
clean_up
5252
echo "Generating huge input for tail; this is slow."
53-
generate_test_file 2000000
53+
generate_test_file 20000
5454
echo "Huge input generated."
5555
tail_test "" 10 "Huge input"
5656

0 commit comments

Comments
 (0)