Commit 3022d16d authored by Dan Good's avatar Dan Good

deque: check HAVE_STATEMENT_EXPR, tweaks from feedback

Thanks to the detailed feedback from David Gibson, I made the
following improvements:

* add missing includes
* check for statement expression support, give an error if absent
* ccanlint directive to skip "without features" steps
* add license ref to top of source files
* rename run1.c test to api1.c
Signed-off-by: default avatarDan Good <dan@dancancode.com>
parent 4eb5f52c
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* Example: * Example:
* // Evaluates arithmetic expressions using Dijkstra's two-stack algorithm. * // Evaluates arithmetic expressions using Dijkstra's two-stack algorithm.
* // Original: http://algs4.cs.princeton.edu/13stacks/EvaluateDeluxe.java.html * // Original: http://algs4.cs.princeton.edu/13stacks/EvaluateDeluxe.java.html
* #define _XOPEN_SOURCE 700 * #define _XOPEN_SOURCE 700 // only for getline(3) in this demo
* #include <stdio.h> * #include <stdio.h>
* #include <stdlib.h> * #include <stdlib.h>
* #include <ctype.h> * #include <ctype.h>
...@@ -122,6 +122,12 @@ ...@@ -122,6 +122,12 @@
* *
* License: APACHE-2 * License: APACHE-2
* Author: Dan Good <dan@dancancode.com> * Author: Dan Good <dan@dancancode.com>
*
* Ccanlint:
* // uses statement expressions
* // supported by gcc, clang, icc, and some others, but not msvc
* // (see https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html)
* objects_build_without_features FAIL
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
......
/* Licensed under Apache License v2.0 - see LICENSE file for details */
#include "config.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
......
#ifndef _DEQUE_H /* Licensed under Apache License v2.0 - see LICENSE file for details */
#define _DEQUE_H #ifndef CCAN_DEQUE_H
#define CCAN_DEQUE_H
#include "config.h"
#if !HAVE_STATEMENT_EXPR
#error "This code needs compiler support for statement expressions. Try using gcc or clang."
#endif
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h>
/** /**
* struct deq - deque metadata * struct deq - deque metadata
...@@ -87,7 +93,7 @@ int deq_resize_(struct deq *q, unsigned n); ...@@ -87,7 +93,7 @@ int deq_resize_(struct deq *q, unsigned n);
* //later * //later
* deq_free(z); * deq_free(z);
* *
* Returns: pointer on success, NULL on error * Returns: 0 on success, -1 on error
*/ */
#define deq_new(w, min, shrink) ({ \ #define deq_new(w, min, shrink) ({ \
w = malloc(sizeof(*w)); \ w = malloc(sizeof(*w)); \
......
#include <ccan/deque/deque.h> #include <ccan/deque/deque.h>
/* Include the C files directly. */
#include <ccan/deque/deque.c>
#include <ccan/tap/tap.h> #include <ccan/tap/tap.h>
int main(void) int main(void)
......
...@@ -12,7 +12,7 @@ int main(void) ...@@ -12,7 +12,7 @@ int main(void)
struct quad { int w, x, y, z; } p, q, r, s, *save; struct quad { int w, x, y, z; } p, q, r, s, *save;
assert(sizeof(struct quad) == sizeof(int) * 4); assert(sizeof(struct quad) == sizeof(int) * 4);
plan_tests(19); plan_tests(20);
typedef DEQ_WRAP(struct quad) qd_t; typedef DEQ_WRAP(struct quad) qd_t;
qd_t a_, *a = &a_; qd_t a_, *a = &a_;
...@@ -52,6 +52,9 @@ int main(void) ...@@ -52,6 +52,9 @@ int main(void)
deq_push(a, q); deq_push(a, q);
ok1(a->v != save && a->deq.head == 0 && a->deq.tail == 5 && a->deq.len == 5 && a->deq.cap == 8); ok1(a->v != save && a->deq.head == 0 && a->deq.tail == 5 && a->deq.len == 5 && a->deq.cap == 8);
ok1(malloc_sz == sizeof(struct quad) * 8); ok1(malloc_sz == sizeof(struct quad) * 8);
save = a->v;
deq_unshift(a, r);
ok1(a->v == save && a->deq.head == 7 && a->deq.tail == 5 && a->deq.len == 6 && a->deq.cap == 8);
deq_reset(a); deq_reset(a);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment