Browse Source

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: Dan Good <dan@dancancode.com>
Dan Good 10 years ago
parent
commit
3022d16de4
5 changed files with 23 additions and 8 deletions
  1. 7 1
      ccan/deque/_info
  2. 2 0
      ccan/deque/deque.c
  3. 10 4
      ccan/deque/deque.h
  4. 0 2
      ccan/deque/test/api1.c
  5. 4 1
      ccan/deque/test/run2.c

+ 7 - 1
ccan/deque/_info

@@ -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 @@
  *
  *
  * 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[])
 {
 {

+ 2 - 0
ccan/deque/deque.c

@@ -1,3 +1,5 @@
+/* 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>

+ 10 - 4
ccan/deque/deque.h

@@ -1,7 +1,13 @@
-#ifndef _DEQUE_H
-#define _DEQUE_H
-
+/* Licensed under Apache License v2.0 - see LICENSE file for details */
+#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);
  *	//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));				\

+ 0 - 2
ccan/deque/test/run1.c → ccan/deque/test/api1.c

@@ -1,6 +1,4 @@
 #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)

+ 4 - 1
ccan/deque/test/run2.c

@@ -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)
 	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);