Module Name:    src
Committed By:   rillig
Date:           Sat Apr 23 17:25:58 UTC 2022

Modified Files:
        src/tests/usr.bin/indent: lsym_lparen_or_lbracket.c token_binary_op.c

Log Message:
tests/indent: add tests for unary and binary operators


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c
cvs rdiff -u -r1.11 -r1.12 src/tests/usr.bin/indent/token_binary_op.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c
diff -u src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.4 src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.5
--- src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c:1.4	Fri Apr 22 21:21:20 2022
+++ src/tests/usr.bin/indent/lsym_lparen_or_lbracket.c	Sat Apr 23 17:25:58 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.4 2022/04/22 21:21:20 rillig Exp $ */
+/* $NetBSD: lsym_lparen_or_lbracket.c,v 1.5 2022/04/23 17:25:58 rillig Exp $ */
 
 /*
  * Tests for the token lsym_lparen_or_lbracket, which represents a '(' or '['
@@ -15,12 +15,102 @@
  * In a 'sizeof' expression, '(' is required if the argument is a type name.
  *
  * After one of the keywords 'for', 'if', 'switch' or 'while', the controlling
- * expression must be enclosed in '(' and ')'.
+ * expression must be enclosed in '(' and ')'; see lsym_for.c, lsym_if.c,
+ * lsym_switch.c, lsym_while.c.
  *
- * In an expression, '(' followed by a type name starts a cast expression.
+ * In an expression, '(' followed by a type name starts a cast expression or
+ * a compound literal.
+ *
+ * In a declaration, '[' derives an array type.
+ *
+ * In an expression, '[' starts an array subscript.
+ */
+
+/* The '(' in a type name derives a function type. */
+#indent input
+typedef void signal_handler(int);
+void (*signal(void (*)(int)))(int);
+#indent end
+
+#indent run
+typedef void signal_handler(int);
+void		(*signal(void (*)(int)))(int);
+#indent end
+
+
+/*
+ * The '(' in an expression overrides operator precedence.  In multi-line
+ * expressions, the continuation lines are aligned on the parentheses.
  */
+#indent input
+int nested = (
+	(
+		(
+			(
+				1 + 4
+			)
+		)
+	)
+);
+#indent end
+
+#indent run
+int		nested = (
+			  (
+			   (
+			    (
+			     1 + 4
+			     )
+			    )
+			   )
+);
+#indent end
+
+
+/* The '(' in a function call expression starts the argument list. */
+#indent input
+int var = macro_call ( arg1,  arg2  ,arg3);
+#indent end
+
+#indent run
+int		var = macro_call(arg1, arg2, arg3);
+#indent end
+
+
+/*
+ * The '(' in a sizeof expression is required for type names and optional for
+ * expressions.
+ */
+#indent input
+size_t sizeof_typename = sizeof ( int );
+size_t sizeof_expr = sizeof ( 12345 ) ;
+#indent end
+
+#indent run
+size_t		sizeof_typename = sizeof(int);
+size_t		sizeof_expr = sizeof(12345);
+#indent end
+
+
+/* The '[' in a type name derives an array type. */
+#indent input
+int array_of_numbers[100];
+#indent end
+
+#indent run
+int		array_of_numbers[100];
+#indent end
+
+
+/* The '[' in an expression accesses an array element. */
+#indent input
+int second_prime = &primes[1];
+#indent end
+
+#indent run
+int		second_prime = &primes[1];
+#indent end
 
-// TODO: Add systematic tests for all cases.
 
 #indent input
 void

Index: src/tests/usr.bin/indent/token_binary_op.c
diff -u src/tests/usr.bin/indent/token_binary_op.c:1.11 src/tests/usr.bin/indent/token_binary_op.c:1.12
--- src/tests/usr.bin/indent/token_binary_op.c:1.11	Sat Apr 23 09:35:26 2022
+++ src/tests/usr.bin/indent/token_binary_op.c	Sat Apr 23 17:25:58 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: token_binary_op.c,v 1.11 2022/04/23 09:35:26 rillig Exp $ */
+/* $NetBSD: token_binary_op.c,v 1.12 2022/04/23 17:25:58 rillig Exp $ */
 
 /*
  * Tests for binary operators like '+', '&&' and several others.
@@ -15,15 +15,10 @@
 void
 punctuators(void)
 {
-	int brackets = array[subscript];
-	int parentheses = function(argument);
 	int braces = { initializer };
 	int period = structure.member;
 	int arrow = structure->member;
 
-	number = function(argument1, argument2);
-	number = function(argument), number;
-
 	/* digraphs */
 	number = array<:subscript:>;
 	number = (int)<% initializer %>;
@@ -34,16 +29,11 @@ punctuators(void)
 void
 punctuators(void)
 {
-	int brackets = array[subscript];
-	int parentheses = function(argument);
 /* $ XXX: The spaces around the initializer are gone. */
 	int braces = {initializer};
 	int period = structure.member;
 	int arrow = structure->member;
 
-	number = function(argument1, argument2);
-	number = function(argument), number;
-
 	/* digraphs */
 /* $ XXX: indent is confused by the digraphs for '[' and ']'. */
 /* $ This probably doesn't matter since digraphs are not used in practice. */
@@ -83,9 +73,9 @@ long_run_of_operators(void)
 
 
 /*
- * For '+' and '-', this does not work since the lexer has to
- * distinguish between '++' and '+' early.  The following sequence is
- * thus tokenized as:
+ * Long chains of '+' and '-' must be split into several operators as the
+ * lexer has to distinguish between '++' and '+' early.  The following
+ * sequence is thus tokenized as:
  *
  *	word		"a"
  *	postfix_op	"++"

Reply via email to