ref: 7a5505a05daf936473c9c6ee0a1d442ac1a920e0
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Dec 22 16:41:25 EST 2019
Squashed 'parg/' content from commit 97f3a07 git-subtree-dir: parg git-subtree-split: 97f3a075109ebace4f660fb341c6b99b2a4b092a
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,14 @@
+*.o
+*.obj
+
+*.a
+*.lib
+
+*.so
+*.so.*
+*.dll
+
+*.exe
+
+/build/
+/doc/
--- /dev/null
+++ b/.travis.yml
@@ -1,0 +1,22 @@
+language: c
+
+compiler: gcc
+
+sudo: false
+
+before_install:
+ - pip install --user codecov
+
+before_script:
+ - mkdir build
+ - cd build
+ - cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_COVERAGE=ON ..
+
+script: cmake --build . && ctest -V .
+
+after_success:
+ - gcov CMakeFiles/parg.dir/parg.c.gcno -o CMakeFiles/parg.dir
+ - codecov -X gcov
+
+notifications:
+ email: false
--- /dev/null
+++ b/CMakeLists.txt
@@ -1,0 +1,56 @@
+cmake_minimum_required(VERSION 2.8.6)
+
+project(parg C)
+
+include(CheckCCompilerFlag)
+include(CTest)
+
+option(BUILD_COVERAGE "Code coverage" OFF)
+
+message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
+
+function(_add_c_compiler_flags var)
+ foreach(flag ${ARGN})
+ string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" flag_var "CFLAG_${flag}")
+ check_c_compiler_flag("${flag}" ${flag_var})
+ if(${flag_var})
+ set(${var} "${${var}} ${flag}")
+ endif()
+ endforeach()
+ set(${var} "${${var}}" PARENT_SCOPE)
+endfunction()
+
+if(MSVC)
+ _add_c_compiler_flags(CMAKE_C_FLAGS "/analyze")
+ add_definitions(/D_CRT_SECURE_NO_WARNINGS)
+elseif(CMAKE_COMPILER_IS_GNUCC)
+ _add_c_compiler_flags(CMAKE_C_FLAGS
+ -Wall
+ -Wextra
+ -pedantic
+ -Wshadow
+ -Wpointer-arith
+ -Wcast-qual
+ -Wcast-align
+ -Wstrict-prototypes
+ -Wmissing-prototypes
+ )
+elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
+ _add_c_compiler_flags(CMAKE_C_FLAGS "-Weverything;-pedantic")
+endif()
+
+if(BUILD_COVERAGE)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage")
+endif()
+
+include_directories(${PROJECT_SOURCE_DIR})
+
+add_library(parg parg.c parg.h)
+
+if(BUILD_TESTING)
+ add_executable(test_parg test/test_parg.c test/greatest.h parg.h)
+ target_link_libraries(test_parg parg)
+
+ enable_testing()
+ add_test(test_parg test_parg)
+endif()
--- /dev/null
+++ b/COPYING
@@ -1,0 +1,113 @@
+CC0 1.0 Universal
+
+Statement of Purpose
+
+The laws of most jurisdictions throughout the world automatically confer
+exclusive Copyright and Related Rights (defined below) upon the creator
+and subsequent owner(s) (each and all, an "owner") of an original work of
+authorship and/or a database (each, a "Work").
+
+Certain owners wish to permanently relinquish those rights to a Work for
+the purpose of contributing to a commons of creative, cultural and
+scientific works ("Commons") that the public can reliably and without fear
+of later claims of infringement build upon, modify, incorporate in other
+works, reuse and redistribute as freely as possible in any form whatsoever
+and for any purposes, including without limitation commercial purposes.
+These owners may contribute to the Commons to promote the ideal of a free
+culture and the further production of creative, cultural and scientific
+works, or to gain reputation or greater distribution for their Work in
+part through the use and efforts of others.
+
+For these and/or other purposes and motivations, and without any
+expectation of additional consideration or compensation, the person
+associating CC0 with a Work (the "Affirmer"), to the extent that he or she
+is an owner of Copyright and Related Rights in the Work, voluntarily
+elects to apply CC0 to the Work and publicly distribute the Work under its
+terms, with knowledge of his or her Copyright and Related Rights in the
+Work and the meaning and intended legal effect of CC0 on those rights.
+
+1. Copyright and Related Rights. A Work made available under CC0 may be
+protected by copyright and related or neighboring rights ("Copyright and
+Related Rights"). Copyright and Related Rights include, but are not
+limited to, the following:
+
+ i. the right to reproduce, adapt, distribute, perform, display,
+ communicate, and translate a Work;
+ ii. moral rights retained by the original author(s) and/or performer(s);
+iii. publicity and privacy rights pertaining to a person's image or
+ likeness depicted in a Work;
+ iv. rights protecting against unfair competition in regards to a Work,
+ subject to the limitations in paragraph 4(a), below;
+ v. rights protecting the extraction, dissemination, use and reuse of data
+ in a Work;
+ vi. database rights (such as those arising under Directive 96/9/EC of the
+ European Parliament and of the Council of 11 March 1996 on the legal
+ protection of databases, and under any national implementation
+ thereof, including any amended or successor version of such
+ directive); and
+vii. other similar, equivalent or corresponding rights throughout the
+ world based on applicable law or treaty, and any national
+ implementations thereof.
+
+2. Waiver. To the greatest extent permitted by, but not in contravention
+of, applicable law, Affirmer hereby overtly, fully, permanently,
+irrevocably and unconditionally waives, abandons, and surrenders all of
+Affirmer's Copyright and Related Rights and associated claims and causes
+of action, whether now known or unknown (including existing as well as
+future claims and causes of action), in the Work (i) in all territories
+worldwide, (ii) for the maximum duration provided by applicable law or
+treaty (including future time extensions), (iii) in any current or future
+medium and for any number of copies, and (iv) for any purpose whatsoever,
+including without limitation commercial, advertising or promotional
+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
+member of the public at large and to the detriment of Affirmer's heirs and
+successors, fully intending that such Waiver shall not be subject to
+revocation, rescission, cancellation, termination, or any other legal or
+equitable action to disrupt the quiet enjoyment of the Work by the public
+as contemplated by Affirmer's express Statement of Purpose.
+
+3. Public License Fallback. Should any part of the Waiver for any reason
+be judged legally invalid or ineffective under applicable law, then the
+Waiver shall be preserved to the maximum extent permitted taking into
+account Affirmer's express Statement of Purpose. In addition, to the
+extent the Waiver is so judged Affirmer hereby grants to each affected
+person a royalty-free, non transferable, non sublicensable, non exclusive,
+irrevocable and unconditional license to exercise Affirmer's Copyright and
+Related Rights in the Work (i) in all territories worldwide, (ii) for the
+maximum duration provided by applicable law or treaty (including future
+time extensions), (iii) in any current or future medium and for any number
+of copies, and (iv) for any purpose whatsoever, including without
+limitation commercial, advertising or promotional purposes (the
+"License"). The License shall be deemed effective as of the date CC0 was
+applied by Affirmer to the Work. Should any part of the License for any
+reason be judged legally invalid or ineffective under applicable law, such
+partial invalidity or ineffectiveness shall not invalidate the remainder
+of the License, and in such case Affirmer hereby affirms that he or she
+will not (i) exercise any of his or her remaining Copyright and Related
+Rights in the Work or (ii) assert any associated claims and causes of
+action with respect to the Work, in either case contrary to Affirmer's
+express Statement of Purpose.
+
+4. Limitations and Disclaimers.
+
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
+ surrendered, licensed or otherwise affected by this document.
+ b. Affirmer offers the Work as-is and makes no representations or
+ warranties of any kind concerning the Work, express, implied,
+ statutory or otherwise, including without limitation warranties of
+ title, merchantability, fitness for a particular purpose, non
+ infringement, or the absence of latent or other defects, accuracy, or
+ the present or absence of errors, whether or not discoverable, all to
+ the greatest extent permissible under applicable law.
+ c. Affirmer disclaims responsibility for clearing rights of other persons
+ that may apply to the Work or any use thereof, including without
+ limitation any person's Copyright and Related Rights in the Work.
+ Further, Affirmer disclaims responsibility for obtaining any necessary
+ consents, permissions or other rights required for any use of the
+ Work.
+ d. Affirmer understands and acknowledges that Creative Commons is not a
+ party to this document and has no duty or obligation with respect to
+ this CC0 or use of the Work.
+
+For more information, please see
+<http://creativecommons.org/publicdomain/zero/1.0/>
--- /dev/null
+++ b/Doxyfile
@@ -1,0 +1,10 @@
+# Doxyfile 1.8.6
+PROJECT_NAME = "parg"
+OUTPUT_DIRECTORY = doc
+JAVADOC_AUTOBRIEF = YES
+TAB_SIZE = 8
+OPTIMIZE_OUTPUT_FOR_C = YES
+EXTRACT_ALL = YES
+INPUT = parg.h README.md
+GENERATE_LATEX = NO
+USE_MDFILE_AS_MAINPAGE = README.md
--- /dev/null
+++ b/README.md
@@ -1,0 +1,205 @@
+
+[![Build Status](https://travis-ci.org/jibsen/parg.svg?branch=master)](https://travis-ci.org/jibsen/parg) [![codecov.io](http://codecov.io/github/jibsen/parg/coverage.svg?branch=master)](http://codecov.io/github/jibsen/parg?branch=master)
+
+About
+-----
+
+Most command-line programs have to parse options, so there are a lot of
+different solutions to this problem. Some offer many features, while others
+are more basic.
+
+One of the simpler solutions for C is the [getopt][] function, and its
+extension `getopt_long`. They iterate over the options in `argv`, returning
+them one at a time on successive calls.
+
+One nice thing about them is that they are available on most Unix-like
+operating systems (and usually accompany GCC elsewhere, like Windows).
+Unfortunately, some implementation details vary between platforms.
+
+A potential question is what license the version you get when you include
+them is available under. Some are GPL, others LGPL. There are also ports of
+`getopt` that use more liberal licenses.
+
+`parg` is a parser for `argv` that works similarly to `getopt`, but does not
+aim to be a direct replacement. It attempts to make some choices about how to
+handle the extensions and idiosyncrasies of other `getopt` implementations,
+and document them.
+
+It consists of a single source and include file, written in portable ANSI C.
+It is made available under [CC0][].
+
+[getopt]: https://en.wikipedia.org/wiki/Getopt
+[CC0]: http://creativecommons.org/publicdomain/zero/1.0/
+
+
+Usage
+-----
+
+The include file `parg.h` contains documentation in the form of [doxygen][]
+comments. A configuration file is included, so you can simply run `doxygen`
+to generate documentation in HTML format.
+
+`parg` uses [CMake][] to generate build systems. To create one for the tools
+on your platform, and build `parg`, use something along the lines of:
+
+~~~sh
+mkdir build
+cd build
+cmake ..
+cmake --build .
+~~~
+
+You can also simply compile the source files and link them into your project.
+CMake just provides an easy way to build and test across various platforms and
+toolsets.
+
+[doxygen]: http://www.doxygen.org/
+[CMake]: http://www.cmake.org/
+
+
+Example
+-------
+
+Here is an example that parses command-line options using `parg_getopt()`:
+
+~~~c
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "parg.h"
+
+int main(int argc, char *argv[])
+{
+ struct parg_state ps;
+ int c;
+
+ parg_init(&ps);
+
+ while ((c = parg_getopt(&ps, argc, argv, "hs:v")) != -1) {
+ switch (c) {
+ case 1:
+ printf("nonoption '%s'\n", ps.optarg);
+ break;
+ case 'h':
+ printf("Usage: testparg [-h] [-v] [-s STRING]\n");
+ return EXIT_SUCCESS;
+ break;
+ case 's':
+ printf("option -s with argument '%s'\n", ps.optarg);
+ break;
+ case 'v':
+ printf("testparg 1.0.0\n");
+ return EXIT_SUCCESS;
+ break;
+ case '?':
+ if (ps.optopt == 's') {
+ printf("option -s requires an argument\n");
+ }
+ else {
+ printf("unknown option -%c\n", ps.optopt);
+ }
+ return EXIT_FAILURE;
+ break;
+ default:
+ printf("error: unhandled option -%c\n", c);
+ return EXIT_FAILURE;
+ break;
+ }
+ }
+
+ for (c = ps.optind; c < argc; ++c) {
+ printf("nonoption '%s'\n", argv[c]);
+ }
+
+ return EXIT_SUCCESS;
+}
+~~~
+
+
+Comparison to `getopt`
+----------------------
+
+### Use of global variables
+
+`getopt` uses global variables to store its state between calls. `parg` uses
+a struct `parg_state`, which you must pass with each call.
+
+### Handling of nonoptions
+
+POSIX and BSD `getopt` return `-1` on the first nonoption argument. GNU
+`getopt` by default reorders `argv` (even though it is passed as const), so
+all options come first.
+
+`parg` does not change `argv`, and returns each nonoption as the option
+argument of an option with value `1` (like GNU `getopt`, if `optstring` were
+prefixed by '`-`').
+
+If you wish to process all options first, and have the nonoptions ordered at
+the end of `argv`, you can use `parg_reorder()`:
+
+~~~c
+ optend = parg_reorder(argc, argv, optstring, NULL);
+
+ while ((c = parg_getopt(&ps, optend, argv, optstring)) != -1) {
+ /* ... */
+ }
+
+ /* elements of argv[] from optend to argc are nonoptions */
+~~~
+
+### Value of `optind` on error
+
+When there are multiple short options in one argument, `getopt` does not
+increment `optind` until the last one is processed. This makes it harder to
+tell which argument an unknown option came from (if `a` is an unknown option,
+`-a` and `-ab` will return '`?`' with different values in `optind`).
+
+`parg` always increments the `optind` value in it's state so it points to the
+next `argv` element to be processed. So when `parg` returns '`?`' (or '`:`'),
+the element that contains the error is `argv[optind - 1]`.
+
+### Value of `optopt` on error
+
+With `getopt_long`, it varies what the values of `optopt` and `longindex` are
+when an error is found with option arguments of long options. Sometimes these
+values are not documented.
+
+`parg` sets `optopt` to `val` if `flag` is `NULL`, and `0` otherwise (which
+equals the return value on successful match), and `longindex` is set to the
+index of the entry in `longopts` that matched.
+
+### Return value on option argument error
+
+When the first character of `optstring` is '`:`', it varies what `getopt`
+returns on extraneous option arguments.
+
+In this case, `parg` returns '`?`' if no option match is found, and '`:`' if
+a match is found, but is missing a required argument, or has an extraneous
+argument.
+
+
+Alternatives
+------------
+
+Some ports of `getopt`:
+
+ - [Free Getopt](http://freegetopt.sourceforge.net/)
+ - [ya_getopt](http://github.com/kubo/ya_getopt/)
+ - [getopt_port](http://github.com/kimgr/getopt_port/)
+
+Other command-line parsing libraries that support C:
+
+ - [Gengetopt](http://www.gnu.org/software/gengetopt/)
+ - [Argp](http://www.gnu.org/software/libc/manual/html_node/Argp.html)
+ - [popt](http://en.wikipedia.org/wiki/Popt)
+ - [argtable](http://argtable.sourceforge.net/)
+ - [optlist](http://michael.dipperstein.com/optlist/)
+ - [Arg_parser](http://www.nongnu.org/arg-parser/arg_parser.html)
+ - [Gopt](http://www.purposeful.co.uk/software/gopt/)
+ - [docopt](http://docopt.org/)
+
+A few C++ command-line parsing libraries:
+
+ - [TCLAP](http://tclap.sourceforge.net/)
+ - [program_options](http://www.boost.org/doc/libs/1_58_0/doc/html/program_options.html)
+ - [CommandLine](http://llvm.org/docs/CommandLine.html)
--- /dev/null
+++ b/parg.c
@@ -1,0 +1,354 @@
+/*
+ * parg - parse argv
+ *
+ * Written in 2015-2016 by Joergen Ibsen
+ *
+ * To the extent possible under law, the author(s) have dedicated all
+ * copyright and related and neighboring rights to this software to the
+ * public domain worldwide. This software is distributed without any
+ * warranty. <http://creativecommons.org/publicdomain/zero/1.0/>
+ */
+
+#include "parg.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Check if state is at end of argv.
+ */
+static int
+is_argv_end(const struct parg_state *ps, int argc, char *const argv[])
+{
+ return ps->optind >= argc || argv[ps->optind] == NULL;
+}
+
+/*
+ * Match nextchar against optstring.
+ */
+static int
+match_short(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring)
+{
+ const char *p = strchr(optstring, *ps->nextchar);
+
+ if (p == NULL) {
+ ps->optopt = *ps->nextchar++;
+ return '?';
+ }
+
+ /* If no option argument, return option */
+ if (p[1] != ':') {
+ return *ps->nextchar++;
+ }
+
+ /* If more characters, return as option argument */
+ if (ps->nextchar[1] != '\0') {
+ ps->optarg = &ps->nextchar[1];
+ ps->nextchar = NULL;
+ return *p;
+ }
+
+ /* If option argument is optional, return option */
+ if (p[2] == ':') {
+ return *ps->nextchar++;
+ }
+
+ /* Option argument required, so return next argv element */
+ if (is_argv_end(ps, argc, argv)) {
+ ps->optopt = *ps->nextchar++;
+ return optstring[0] == ':' ? ':' : '?';
+ }
+
+ ps->optarg = argv[ps->optind++];
+ ps->nextchar = NULL;
+ return *p;
+}
+
+/*
+ * Match string at nextchar against longopts.
+ */
+static int
+match_long(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring,
+ const struct parg_option *longopts, int *longindex)
+{
+ size_t len;
+ int num_match = 0;
+ int match = -1;
+ int i;
+
+ len = strcspn(ps->nextchar, "=");
+
+ for (i = 0; longopts[i].name; ++i) {
+ if (strncmp(ps->nextchar, longopts[i].name, len) == 0) {
+ match = i;
+ num_match++;
+ /* Take if exact match */
+ if (longopts[i].name[len] == '\0') {
+ num_match = 1;
+ break;
+ }
+ }
+ }
+
+ /* Return '?' on no or ambiguous match */
+ if (num_match != 1) {
+ ps->optopt = 0;
+ ps->nextchar = NULL;
+ return '?';
+ }
+
+ assert(match != -1);
+
+ if (longindex) {
+ *longindex = match;
+ }
+
+ if (ps->nextchar[len] == '=') {
+ /* Option argument present, check if extraneous */
+ if (longopts[match].has_arg == PARG_NOARG) {
+ ps->optopt = longopts[match].flag ? 0 : longopts[match].val;
+ ps->nextchar = NULL;
+ return optstring[0] == ':' ? ':' : '?';
+ }
+ else {
+ ps->optarg = &ps->nextchar[len + 1];
+ }
+ }
+ else if (longopts[match].has_arg == PARG_REQARG) {
+ /* Option argument required, so return next argv element */
+ if (is_argv_end(ps, argc, argv)) {
+ ps->optopt = longopts[match].flag ? 0 : longopts[match].val;
+ ps->nextchar = NULL;
+ return optstring[0] == ':' ? ':' : '?';
+ }
+
+ ps->optarg = argv[ps->optind++];
+ }
+
+ ps->nextchar = NULL;
+
+ if (longopts[match].flag != NULL) {
+ *longopts[match].flag = longopts[match].val;
+ return 0;
+ }
+
+ return longopts[match].val;
+}
+
+void
+parg_init(struct parg_state *ps)
+{
+ ps->optarg = NULL;
+ ps->optind = 1;
+ ps->optopt = '?';
+ ps->nextchar = NULL;
+}
+
+int
+parg_getopt(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring)
+{
+ return parg_getopt_long(ps, argc, argv, optstring, NULL, NULL);
+}
+
+int
+parg_getopt_long(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring,
+ const struct parg_option *longopts, int *longindex)
+{
+ assert(ps != NULL);
+ assert(argv != NULL);
+ assert(optstring != NULL);
+
+ ps->optarg = NULL;
+
+ if (argc < 2) {
+ return -1;
+ }
+
+ /* Advance to next element if needed */
+ if (ps->nextchar == NULL || *ps->nextchar == '\0') {
+ if (is_argv_end(ps, argc, argv)) {
+ return -1;
+ }
+
+ ps->nextchar = argv[ps->optind++];
+
+ /* Check for nonoption element (including '-') */
+ if (ps->nextchar[0] != '-' || ps->nextchar[1] == '\0') {
+ ps->optarg = ps->nextchar;
+ ps->nextchar = NULL;
+ return 1;
+ }
+
+ /* Check for '--' */
+ if (ps->nextchar[1] == '-') {
+ if (ps->nextchar[2] == '\0') {
+ ps->nextchar = NULL;
+ return -1;
+ }
+
+ if (longopts != NULL) {
+ ps->nextchar += 2;
+
+ return match_long(ps, argc, argv, optstring,
+ longopts, longindex);
+ }
+ }
+
+ ps->nextchar++;
+ }
+
+ /* Match nextchar */
+ return match_short(ps, argc, argv, optstring);
+}
+
+/*
+ * Reverse elements of `v` from `i` to `j`.
+ */
+static void
+reverse(char *v[], int i, int j)
+{
+ while (j - i > 1) {
+ char *tmp = v[i];
+ v[i] = v[j - 1];
+ v[j - 1] = tmp;
+ ++i;
+ --j;
+ }
+}
+
+/*
+ * Reorder elements of `argv` with no special cases.
+ *
+ * This function assumes there is no `--` element, and the last element
+ * is not an option missing a required argument.
+ *
+ * The algorithm is described here:
+ * http://hardtoc.com/2016/11/07/reordering-arguments.html
+ */
+static int
+parg_reorder_simple(int argc, char *argv[],
+ const char *optstring,
+ const struct parg_option *longopts)
+{
+ struct parg_state ps;
+ int change;
+ int l = 0;
+ int m = 0;
+ int r = 0;
+
+ if (argc < 2) {
+ return argc;
+ }
+
+ do {
+ int nextind;
+ int c;
+
+ parg_init(&ps);
+
+ nextind = ps.optind;
+
+ /* Parse until end of argument */
+ do {
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+ } while (ps.nextchar != NULL && *ps.nextchar != '\0');
+
+ change = 0;
+
+ do {
+ /* Find next non-option */
+ for (l = nextind; c != 1 && c != -1;) {
+ l = ps.optind;
+
+ do {
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+ } while (ps.nextchar != NULL && *ps.nextchar != '\0');
+ }
+
+ /* Find next option */
+ for (m = l; c == 1;) {
+ m = ps.optind;
+
+ do {
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+ } while (ps.nextchar != NULL && *ps.nextchar != '\0');
+ }
+
+ /* Find next non-option */
+ for (r = m; c != 1 && c != -1;) {
+ r = ps.optind;
+
+ do {
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+ } while (ps.nextchar != NULL && *ps.nextchar != '\0');
+ }
+
+ /* Find next option */
+ for (nextind = r; c == 1;) {
+ nextind = ps.optind;
+
+ do {
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+ } while (ps.nextchar != NULL && *ps.nextchar != '\0');
+ }
+
+ if (m < r) {
+ change = 1;
+ reverse(argv, l, m);
+ reverse(argv, m, r);
+ reverse(argv, l, r);
+ }
+ } while (c != -1);
+ } while (change != 0);
+
+ return l + (r - m);
+}
+
+int
+parg_reorder(int argc, char *argv[],
+ const char *optstring,
+ const struct parg_option *longopts)
+{
+ struct parg_state ps;
+ int lastind;
+ int optend;
+ int c;
+
+ assert(argv != NULL);
+ assert(optstring != NULL);
+
+ if (argc < 2) {
+ return argc;
+ }
+
+ parg_init(&ps);
+
+ /* Find end of normal arguments */
+ do {
+ lastind = ps.optind;
+
+ c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+
+ /* Check for trailing option with error */
+ if ((c == '?' || c == ':') && is_argv_end(&ps, argc, argv)) {
+ lastind = ps.optind - 1;
+ break;
+ }
+ } while (c != -1);
+
+ optend = parg_reorder_simple(lastind, argv, optstring, longopts);
+
+ /* Rotate `--` or trailing option with error into position */
+ if (lastind < argc) {
+ reverse(argv, optend, lastind);
+ reverse(argv, optend, lastind + 1);
+ ++optend;
+ }
+
+ return optend;
+}
--- /dev/null
+++ b/parg.h
@@ -1,0 +1,192 @@
+/*
+ * parg - parse argv
+ *
+ * Written in 2015-2016 by Joergen Ibsen
+ *
+ * To the extent possible under law, the author(s) have dedicated all
+ * copyright and related and neighboring rights to this software to the
+ * public domain worldwide. This software is distributed without any
+ * warranty. <http://creativecommons.org/publicdomain/zero/1.0/>
+ */
+
+#ifndef PARG_H_INCLUDED
+#define PARG_H_INCLUDED
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PARG_VER_MAJOR 1 /**< Major version number */
+#define PARG_VER_MINOR 0 /**< Minor version number */
+#define PARG_VER_PATCH 2 /**< Patch version number */
+#define PARG_VER_STRING "1.0.2" /**< Version number as a string */
+
+/**
+ * Structure containing state between calls to parser.
+ *
+ * @see parg_init
+ */
+struct parg_state {
+ const char *optarg; /**< Pointer to option argument, if any */
+ int optind; /**< Next index in argv to process */
+ int optopt; /**< Option value resulting in error, if any */
+ const char *nextchar; /**< Next character to process */
+};
+
+/**
+ * Structure for supplying long options to `parg_getopt_long()`.
+ *
+ * @see parg_getopt_long
+ */
+struct parg_option {
+ const char *name; /**< Name of option */
+ int has_arg; /**< Option argument status */
+ int *flag; /**< Pointer to flag variable */
+ int val; /**< Value of option */
+};
+
+/**
+ * Values for `has_arg` flag in `parg_option`.
+ *
+ * @see parg_option
+ */
+typedef enum {
+ PARG_NOARG, /**< No argument */
+ PARG_REQARG, /**< Required argument */
+ PARG_OPTARG /**< Optional argument */
+} parg_arg_num;
+
+/**
+ * Initialize `ps`.
+ *
+ * Must be called before using state with a parser.
+ *
+ * @see parg_state
+ *
+ * @param ps pointer to state
+ */
+void
+parg_init(struct parg_state *ps);
+
+/**
+ * Parse next short option in `argv`.
+ *
+ * Elements in `argv` that contain short options start with a single dash
+ * followed by one or more option characters, and optionally an option
+ * argument for the last option character. Examples are '`-d`', '`-ofile`',
+ * and '`-dofile`'.
+ *
+ * Consecutive calls to this function match the command-line arguments in
+ * `argv` against the short option characters in `optstring`.
+ *
+ * If an option character in `optstring` is followed by a colon, '`:`', the
+ * option requires an argument. If it is followed by two colons, the option
+ * may take an optional argument.
+ *
+ * If a match is found, `optarg` points to the option argument, if any, and
+ * the value of the option character is returned.
+ *
+ * If a match is found, but is missing a required option argument, `optopt`
+ * is set to the option character. If the first character in `optstring` is
+ * '`:`', then '`:`' is returned, otherwise '`?`' is returned.
+ *
+ * If no option character in `optstring` matches a short option, `optopt`
+ * is set to the option character, and '`?`' is returned.
+ *
+ * If an element of argv does not contain options (a nonoption element),
+ * `optarg` points to the element, and `1` is returned.
+ *
+ * An element consisting of a single dash, '`-`', is returned as a nonoption.
+ *
+ * Parsing stops and `-1` is returned, when the end of `argv` is reached, or
+ * if an element contains '`--`'.
+ *
+ * Works similarly to `getopt`, if `optstring` were prefixed by '`-`'.
+ *
+ * @param ps pointer to state
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @return option value on match, `1` on nonoption element, `-1` on end of
+ * arguments, '`?`' on unmatched option, '`?`' or '`:`' on option argument
+ * error
+ */
+int
+parg_getopt(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring);
+
+/**
+ * Parse next long or short option in `argv`.
+ *
+ * Elements in `argv` that contain a long option start with two dashes
+ * followed by a string, and optionally an equal sign and an option argument.
+ * Examples are '`--help`' and '`--size=5`'.
+ *
+ * If no exact match is found, an unambiguous prefix of a long option will
+ * match. For example, if '`foo`' and '`foobar`' are valid long options, then
+ * '`--fo`' is ambiguous and will not match, '`--foo`' matches exactly, and
+ * '`--foob`' is an unambiguous prefix and will match.
+ *
+ * If a long option match is found, and `flag` is `NULL`, `val` is returned.
+ *
+ * If a long option match is found, and `flag` is not `NULL`, `val` is stored
+ * in the variable `flag` points to, and `0` is returned.
+ *
+ * If a long option match is found, but is missing a required option argument,
+ * or has an option argument even though it takes none, `optopt` is set to
+ * `val` if `flag` is `NULL`, and `0` otherwise. If the first character in
+ * `optstring` is '`:`', then '`:`' is returned, otherwise '`?`' is returned.
+ *
+ * If `longindex` is not `NULL`, the index of the entry in `longopts` that
+ * matched is stored there.
+ *
+ * If no long option in `longopts` matches a long option, '`?`' is returned.
+ *
+ * Handling of nonoptions and short options is like `parg_getopt()`.
+ *
+ * If no short options are required, an empty string, `""`, should be passed
+ * as `optstring`.
+ *
+ * Works similarly to `getopt_long`, if `optstring` were prefixed by '`-`'.
+ *
+ * @see parg_getopt
+ *
+ * @param ps pointer to state
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @param longopts array of `parg_option` structures
+ * @param longindex pointer to variable to store index of matching option in
+ * @return option value on match, `0` for flag option, `1` on nonoption
+ * element, `-1` on end of arguments, '`?`' on unmatched or ambiguous option,
+ * '`?`' or '`:`' on option argument error
+ */
+int
+parg_getopt_long(struct parg_state *ps, int argc, char *const argv[],
+ const char *optstring,
+ const struct parg_option *longopts, int *longindex);
+
+/**
+ * Reorder elements of `argv` so options appear first.
+ *
+ * If there are no long options, `longopts` may be `NULL`.
+ *
+ * The return value can be used as `argc` parameter for `parg_getopt()` and
+ * `parg_getopt_long()`.
+ *
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @param longopts array of `parg_option` structures
+ * @return index of first nonoption in `argv` on success, `-1` on error
+ */
+int
+parg_reorder(int argc, char *argv[],
+ const char *optstring,
+ const struct parg_option *longopts);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* PARG_H_INCLUDED */
--- /dev/null
+++ b/test/greatest.h
@@ -1,0 +1,1035 @@
+/*
+ * Copyright (c) 2011-2016 Scott Vokes <vokes.s@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef GREATEST_H
+#define GREATEST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* 1.2.1 */
+#define GREATEST_VERSION_MAJOR 1
+#define GREATEST_VERSION_MINOR 2
+#define GREATEST_VERSION_PATCH 1
+
+/* A unit testing system for C, contained in 1 file.
+ * It doesn't use dynamic allocation or depend on anything
+ * beyond ANSI C89.
+ *
+ * An up-to-date version can be found at:
+ * https://github.com/silentbicycle/greatest/
+ */
+
+
+/*********************************************************************
+ * Minimal test runner template
+ *********************************************************************/
+#if 0
+
+#include "greatest.h"
+
+TEST foo_should_foo(void) {
+ PASS();
+}
+
+static void setup_cb(void *data) {
+ printf("setup callback for each test case\n");
+}
+
+static void teardown_cb(void *data) {
+ printf("teardown callback for each test case\n");
+}
+
+SUITE(suite) {
+ /* Optional setup/teardown callbacks which will be run before/after
+ * every test case. If using a test suite, they will be cleared when
+ * the suite finishes. */
+ SET_SETUP(setup_cb, voidp_to_callback_data);
+ SET_TEARDOWN(teardown_cb, voidp_to_callback_data);
+
+ RUN_TEST(foo_should_foo);
+}
+
+/* Add definitions that need to be in the test runner's main file. */
+GREATEST_MAIN_DEFS();
+
+/* Set up, run suite(s) of tests, report pass/fail/skip stats. */
+int run_tests(void) {
+ GREATEST_INIT(); /* init. greatest internals */
+ /* List of suites to run (if any). */
+ RUN_SUITE(suite);
+
+ /* Tests can also be run directly, without using test suites. */
+ RUN_TEST(foo_should_foo);
+
+ GREATEST_PRINT_REPORT(); /* display results */
+ return greatest_all_passed();
+}
+
+/* main(), for a standalone command-line test runner.
+ * This replaces run_tests above, and adds command line option
+ * handling and exiting with a pass/fail status. */
+int main(int argc, char **argv) {
+ GREATEST_MAIN_BEGIN(); /* init & parse command-line args */
+ RUN_SUITE(suite);
+ GREATEST_MAIN_END(); /* display results */
+}
+
+#endif
+/*********************************************************************/
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+/***********
+ * Options *
+ ***********/
+
+/* Default column width for non-verbose output. */
+#ifndef GREATEST_DEFAULT_WIDTH
+#define GREATEST_DEFAULT_WIDTH 72
+#endif
+
+/* FILE *, for test logging. */
+#ifndef GREATEST_STDOUT
+#define GREATEST_STDOUT stdout
+#endif
+
+/* Remove GREATEST_ prefix from most commonly used symbols? */
+#ifndef GREATEST_USE_ABBREVS
+#define GREATEST_USE_ABBREVS 1
+#endif
+
+/* Set to 0 to disable all use of setjmp/longjmp. */
+#ifndef GREATEST_USE_LONGJMP
+#define GREATEST_USE_LONGJMP 1
+#endif
+
+#if GREATEST_USE_LONGJMP
+#include <setjmp.h>
+#endif
+
+/* Set to 0 to disable all use of time.h / clock(). */
+#ifndef GREATEST_USE_TIME
+#define GREATEST_USE_TIME 1
+#endif
+
+#if GREATEST_USE_TIME
+#include <time.h>
+#endif
+
+/* Floating point type, for ASSERT_IN_RANGE. */
+#ifndef GREATEST_FLOAT
+#define GREATEST_FLOAT double
+#define GREATEST_FLOAT_FMT "%g"
+#endif
+
+/*********
+ * Types *
+ *********/
+
+/* Info for the current running suite. */
+typedef struct greatest_suite_info {
+ unsigned int tests_run;
+ unsigned int passed;
+ unsigned int failed;
+ unsigned int skipped;
+
+#if GREATEST_USE_TIME
+ /* timers, pre/post running suite and individual tests */
+ clock_t pre_suite;
+ clock_t post_suite;
+ clock_t pre_test;
+ clock_t post_test;
+#endif
+} greatest_suite_info;
+
+/* Type for a suite function. */
+typedef void (greatest_suite_cb)(void);
+
+/* Types for setup/teardown callbacks. If non-NULL, these will be run
+ * and passed the pointer to their additional data. */
+typedef void (greatest_setup_cb)(void *udata);
+typedef void (greatest_teardown_cb)(void *udata);
+
+/* Type for an equality comparison between two pointers of the same type.
+ * Should return non-0 if equal, otherwise 0.
+ * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
+typedef int greatest_equal_cb(const void *exp, const void *got, void *udata);
+
+/* Type for a callback that prints a value pointed to by T.
+ * Return value has the same meaning as printf's.
+ * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
+typedef int greatest_printf_cb(const void *t, void *udata);
+
+/* Callbacks for an arbitrary type; needed for type-specific
+ * comparisons via GREATEST_ASSERT_EQUAL_T[m].*/
+typedef struct greatest_type_info {
+ greatest_equal_cb *equal;
+ greatest_printf_cb *print;
+} greatest_type_info;
+
+typedef struct greatest_memory_cmp_env {
+ const unsigned char *exp;
+ const unsigned char *got;
+ size_t size;
+} greatest_memory_cmp_env;
+
+/* Callbacks for string and raw memory types. */
+extern greatest_type_info greatest_type_info_string;
+extern greatest_type_info greatest_type_info_memory;
+
+typedef enum {
+ GREATEST_FLAG_FIRST_FAIL = 0x01,
+ GREATEST_FLAG_LIST_ONLY = 0x02
+} greatest_flag_t;
+
+/* Struct containing all test runner state. */
+typedef struct greatest_run_info {
+ unsigned char flags;
+ unsigned char verbosity;
+ unsigned int tests_run; /* total test count */
+
+ /* overall pass/fail/skip counts */
+ unsigned int passed;
+ unsigned int failed;
+ unsigned int skipped;
+ unsigned int assertions;
+
+ /* currently running test suite */
+ greatest_suite_info suite;
+
+ /* info to print about the most recent failure */
+ const char *fail_file;
+ unsigned int fail_line;
+ const char *msg;
+
+ /* current setup/teardown hooks and userdata */
+ greatest_setup_cb *setup;
+ void *setup_udata;
+ greatest_teardown_cb *teardown;
+ void *teardown_udata;
+
+ /* formatting info for ".....s...F"-style output */
+ unsigned int col;
+ unsigned int width;
+
+ /* only run a specific suite or test */
+ const char *suite_filter;
+ const char *test_filter;
+
+#if GREATEST_USE_TIME
+ /* overall timers */
+ clock_t begin;
+ clock_t end;
+#endif
+
+#if GREATEST_USE_LONGJMP
+ jmp_buf jump_dest;
+#endif
+} greatest_run_info;
+
+struct greatest_report_t {
+ /* overall pass/fail/skip counts */
+ unsigned int passed;
+ unsigned int failed;
+ unsigned int skipped;
+ unsigned int assertions;
+};
+
+/* Global var for the current testing context.
+ * Initialized by GREATEST_MAIN_DEFS(). */
+extern greatest_run_info greatest_info;
+
+/* Type for ASSERT_ENUM_EQ's ENUM_STR argument. */
+typedef const char *greatest_enum_str_fun(int value);
+
+/**********************
+ * Exported functions *
+ **********************/
+
+/* These are used internally by greatest. */
+void greatest_do_pass(const char *name);
+void greatest_do_fail(const char *name);
+void greatest_do_skip(const char *name);
+int greatest_pre_test(const char *name);
+void greatest_post_test(const char *name, int res);
+void greatest_usage(const char *name);
+int greatest_do_assert_equal_t(const void *exp, const void *got,
+ greatest_type_info *type_info, void *udata);
+
+/* These are part of the public greatest API. */
+void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata);
+void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata);
+int greatest_all_passed(void);
+void greatest_set_test_filter(const char *name);
+void greatest_set_suite_filter(const char *name);
+void greatest_get_report(struct greatest_report_t *report);
+unsigned int greatest_get_verbosity(void);
+void greatest_set_verbosity(unsigned int verbosity);
+void greatest_set_flag(greatest_flag_t flag);
+
+
+/********************
+* Language Support *
+********************/
+
+/* If __VA_ARGS__ (C99) is supported, allow parametric testing
+* without needing to manually manage the argument struct. */
+#if __STDC_VERSION__ >= 19901L || _MSC_VER >= 1800
+#define GREATEST_VA_ARGS
+#endif
+
+
+/**********
+ * Macros *
+ **********/
+
+/* Define a suite. */
+#define GREATEST_SUITE(NAME) void NAME(void); void NAME(void)
+
+/* Declare a suite, provided by another compilation unit. */
+#define GREATEST_SUITE_EXTERN(NAME) void NAME(void)
+
+/* Start defining a test function.
+ * The arguments are not included, to allow parametric testing. */
+#define GREATEST_TEST static enum greatest_test_res
+
+/* PASS/FAIL/SKIP result from a test. Used internally. */
+typedef enum greatest_test_res {
+ GREATEST_TEST_RES_PASS = 0,
+ GREATEST_TEST_RES_FAIL = -1,
+ GREATEST_TEST_RES_SKIP = 1
+} greatest_test_res;
+
+/* Run a suite. */
+#define GREATEST_RUN_SUITE(S_NAME) greatest_run_suite(S_NAME, #S_NAME)
+
+/* Run a test in the current suite. */
+#define GREATEST_RUN_TEST(TEST) \
+ do { \
+ if (greatest_pre_test(#TEST) == 1) { \
+ enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \
+ if (res == GREATEST_TEST_RES_PASS) { \
+ res = TEST(); \
+ } \
+ greatest_post_test(#TEST, res); \
+ } else if (GREATEST_LIST_ONLY()) { \
+ fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
+ } \
+ } while (0)
+
+/* Ignore a test, don't warn about it being unused. */
+#define GREATEST_IGNORE_TEST(TEST) (void)TEST
+
+/* Run a test in the current suite with one void * argument,
+ * which can be a pointer to a struct with multiple arguments. */
+#define GREATEST_RUN_TEST1(TEST, ENV) \
+ do { \
+ if (greatest_pre_test(#TEST) == 1) { \
+ int res = TEST(ENV); \
+ greatest_post_test(#TEST, res); \
+ } else if (GREATEST_LIST_ONLY()) { \
+ fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
+ } \
+ } while (0)
+
+#ifdef GREATEST_VA_ARGS
+#define GREATEST_RUN_TESTp(TEST, ...) \
+ do { \
+ if (greatest_pre_test(#TEST) == 1) { \
+ int res = TEST(__VA_ARGS__); \
+ greatest_post_test(#TEST, res); \
+ } else if (GREATEST_LIST_ONLY()) { \
+ fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
+ } \
+ } while (0)
+#endif
+
+
+/* Check if the test runner is in verbose mode. */
+#define GREATEST_IS_VERBOSE() ((greatest_info.verbosity) > 0)
+#define GREATEST_LIST_ONLY() \
+ (greatest_info.flags & GREATEST_FLAG_LIST_ONLY)
+#define GREATEST_FIRST_FAIL() \
+ (greatest_info.flags & GREATEST_FLAG_FIRST_FAIL)
+#define GREATEST_FAILURE_ABORT() \
+ (greatest_info.suite.failed > 0 && GREATEST_FIRST_FAIL())
+
+/* Message-less forms of tests defined below. */
+#define GREATEST_PASS() GREATEST_PASSm(NULL)
+#define GREATEST_FAIL() GREATEST_FAILm(NULL)
+#define GREATEST_SKIP() GREATEST_SKIPm(NULL)
+#define GREATEST_ASSERT(COND) \
+ GREATEST_ASSERTm(#COND, COND)
+#define GREATEST_ASSERT_OR_LONGJMP(COND) \
+ GREATEST_ASSERT_OR_LONGJMPm(#COND, COND)
+#define GREATEST_ASSERT_FALSE(COND) \
+ GREATEST_ASSERT_FALSEm(#COND, COND)
+#define GREATEST_ASSERT_EQ(EXP, GOT) \
+ GREATEST_ASSERT_EQm(#EXP " != " #GOT, EXP, GOT)
+#define GREATEST_ASSERT_EQ_FMT(EXP, GOT, FMT) \
+ GREATEST_ASSERT_EQ_FMTm(#EXP " != " #GOT, EXP, GOT, FMT)
+#define GREATEST_ASSERT_IN_RANGE(EXP, GOT, TOL) \
+ GREATEST_ASSERT_IN_RANGEm(#EXP " != " #GOT " +/- " #TOL, EXP, GOT, TOL)
+#define GREATEST_ASSERT_EQUAL_T(EXP, GOT, TYPE_INFO, UDATA) \
+ GREATEST_ASSERT_EQUAL_Tm(#EXP " != " #GOT, EXP, GOT, TYPE_INFO, UDATA)
+#define GREATEST_ASSERT_STR_EQ(EXP, GOT) \
+ GREATEST_ASSERT_STR_EQm(#EXP " != " #GOT, EXP, GOT)
+#define GREATEST_ASSERT_STRN_EQ(EXP, GOT, SIZE) \
+ GREATEST_ASSERT_STRN_EQm(#EXP " != " #GOT, EXP, GOT, SIZE)
+#define GREATEST_ASSERT_MEM_EQ(EXP, GOT, SIZE) \
+ GREATEST_ASSERT_MEM_EQm(#EXP " != " #GOT, EXP, GOT, SIZE)
+#define GREATEST_ASSERT_ENUM_EQ(EXP, GOT, ENUM_STR) \
+ GREATEST_ASSERT_ENUM_EQm(#EXP " != " #GOT, EXP, GOT, ENUM_STR)
+
+/* The following forms take an additional message argument first,
+ * to be displayed by the test runner. */
+
+/* Fail if a condition is not true, with message. */
+#define GREATEST_ASSERTm(MSG, COND) \
+ do { \
+ greatest_info.assertions++; \
+ if (!(COND)) { GREATEST_FAILm(MSG); } \
+ } while (0)
+
+/* Fail if a condition is not true, longjmping out of test. */
+#define GREATEST_ASSERT_OR_LONGJMPm(MSG, COND) \
+ do { \
+ greatest_info.assertions++; \
+ if (!(COND)) { GREATEST_FAIL_WITH_LONGJMPm(MSG); } \
+ } while (0)
+
+/* Fail if a condition is not false, with message. */
+#define GREATEST_ASSERT_FALSEm(MSG, COND) \
+ do { \
+ greatest_info.assertions++; \
+ if ((COND)) { GREATEST_FAILm(MSG); } \
+ } while (0)
+
+/* Fail if EXP != GOT (equality comparison by ==). */
+#define GREATEST_ASSERT_EQm(MSG, EXP, GOT) \
+ do { \
+ greatest_info.assertions++; \
+ if ((EXP) != (GOT)) { GREATEST_FAILm(MSG); } \
+ } while (0)
+
+/* Fail if EXP != GOT (equality comparison by ==).
+ * Warning: EXP and GOT will be evaluated more than once on failure. */
+#define GREATEST_ASSERT_EQ_FMTm(MSG, EXP, GOT, FMT) \
+ do { \
+ const char *greatest_FMT = ( FMT ); \
+ greatest_info.assertions++; \
+ if ((EXP) != (GOT)) { \
+ fprintf(GREATEST_STDOUT, "\nExpected: "); \
+ fprintf(GREATEST_STDOUT, greatest_FMT, EXP); \
+ fprintf(GREATEST_STDOUT, "\n Got: "); \
+ fprintf(GREATEST_STDOUT, greatest_FMT, GOT); \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ GREATEST_FAILm(MSG); \
+ } \
+ } while (0)
+
+/* Fail if EXP is not equal to GOT, printing enum IDs. */
+#define GREATEST_ASSERT_ENUM_EQm(MSG, EXP, GOT, ENUM_STR) \
+ do { \
+ int greatest_EXP = (int)(EXP); \
+ int greatest_GOT = (int)(GOT); \
+ greatest_enum_str_fun *greatest_ENUM_STR = ENUM_STR; \
+ if (greatest_EXP != greatest_GOT) { \
+ fprintf(GREATEST_STDOUT, "\nExpected: %s", \
+ greatest_ENUM_STR(greatest_EXP)); \
+ fprintf(GREATEST_STDOUT, "\n Got: %s\n", \
+ greatest_ENUM_STR(greatest_GOT)); \
+ GREATEST_FAILm(MSG); \
+ } \
+ } while (0) \
+
+/* Fail if GOT not in range of EXP +|- TOL. */
+#define GREATEST_ASSERT_IN_RANGEm(MSG, EXP, GOT, TOL) \
+ do { \
+ GREATEST_FLOAT greatest_EXP = (EXP); \
+ GREATEST_FLOAT greatest_GOT = (GOT); \
+ GREATEST_FLOAT greatest_TOL = (TOL); \
+ greatest_info.assertions++; \
+ if ((greatest_EXP > greatest_GOT && \
+ greatest_EXP - greatest_GOT > greatest_TOL) || \
+ (greatest_EXP < greatest_GOT && \
+ greatest_GOT - greatest_EXP > greatest_TOL)) { \
+ fprintf(GREATEST_STDOUT, \
+ "\nExpected: " GREATEST_FLOAT_FMT \
+ " +/- " GREATEST_FLOAT_FMT \
+ "\n Got: " GREATEST_FLOAT_FMT \
+ "\n", \
+ greatest_EXP, greatest_TOL, greatest_GOT); \
+ GREATEST_FAILm(MSG); \
+ } \
+ } while (0)
+
+/* Fail if EXP is not equal to GOT, according to strcmp. */
+#define GREATEST_ASSERT_STR_EQm(MSG, EXP, GOT) \
+ do { \
+ GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \
+ &greatest_type_info_string, NULL); \
+ } while (0) \
+
+/* Fail if EXP is not equal to GOT, according to strcmp. */
+#define GREATEST_ASSERT_STRN_EQm(MSG, EXP, GOT, SIZE) \
+ do { \
+ size_t size = SIZE; \
+ GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \
+ &greatest_type_info_string, &size); \
+ } while (0) \
+
+/* Fail if EXP is not equal to GOT, according to memcmp. */
+#define GREATEST_ASSERT_MEM_EQm(MSG, EXP, GOT, SIZE) \
+ do { \
+ greatest_memory_cmp_env env; \
+ env.exp = (const unsigned char *)EXP; \
+ env.got = (const unsigned char *)GOT; \
+ env.size = SIZE; \
+ GREATEST_ASSERT_EQUAL_Tm(MSG, env.exp, env.got, \
+ &greatest_type_info_memory, &env); \
+ } while (0) \
+
+/* Fail if EXP is not equal to GOT, according to a comparison
+ * callback in TYPE_INFO. If they are not equal, optionally use a
+ * print callback in TYPE_INFO to print them. */
+#define GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, TYPE_INFO, UDATA) \
+ do { \
+ greatest_type_info *type_info = (TYPE_INFO); \
+ greatest_info.assertions++; \
+ if (!greatest_do_assert_equal_t(EXP, GOT, \
+ type_info, UDATA)) { \
+ if (type_info == NULL || type_info->equal == NULL) { \
+ GREATEST_FAILm("type_info->equal callback missing!"); \
+ } else { \
+ GREATEST_FAILm(MSG); \
+ } \
+ } \
+ } while (0) \
+
+/* Pass. */
+#define GREATEST_PASSm(MSG) \
+ do { \
+ greatest_info.msg = MSG; \
+ return GREATEST_TEST_RES_PASS; \
+ } while (0)
+
+/* Fail. */
+#define GREATEST_FAILm(MSG) \
+ do { \
+ greatest_info.fail_file = __FILE__; \
+ greatest_info.fail_line = __LINE__; \
+ greatest_info.msg = MSG; \
+ return GREATEST_TEST_RES_FAIL; \
+ } while (0)
+
+/* Optional GREATEST_FAILm variant that longjmps. */
+#if GREATEST_USE_LONGJMP
+#define GREATEST_FAIL_WITH_LONGJMP() GREATEST_FAIL_WITH_LONGJMPm(NULL)
+#define GREATEST_FAIL_WITH_LONGJMPm(MSG) \
+ do { \
+ greatest_info.fail_file = __FILE__; \
+ greatest_info.fail_line = __LINE__; \
+ greatest_info.msg = MSG; \
+ longjmp(greatest_info.jump_dest, GREATEST_TEST_RES_FAIL); \
+ } while (0)
+#endif
+
+/* Skip the current test. */
+#define GREATEST_SKIPm(MSG) \
+ do { \
+ greatest_info.msg = MSG; \
+ return GREATEST_TEST_RES_SKIP; \
+ } while (0)
+
+/* Check the result of a subfunction using ASSERT, etc. */
+#define GREATEST_CHECK_CALL(RES) \
+ do { \
+ enum greatest_test_res greatest_RES = RES; \
+ if (greatest_RES != GREATEST_TEST_RES_PASS) { \
+ return greatest_RES; \
+ } \
+ } while (0) \
+
+#if GREATEST_USE_TIME
+#define GREATEST_SET_TIME(NAME) \
+ NAME = clock(); \
+ if (NAME == (clock_t) -1) { \
+ fprintf(GREATEST_STDOUT, \
+ "clock error: %s\n", #NAME); \
+ exit(EXIT_FAILURE); \
+ }
+
+#define GREATEST_CLOCK_DIFF(C1, C2) \
+ fprintf(GREATEST_STDOUT, " (%lu ticks, %.3f sec)", \
+ (long unsigned int) (C2) - (long unsigned int)(C1), \
+ (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC))
+#else
+#define GREATEST_SET_TIME(UNUSED)
+#define GREATEST_CLOCK_DIFF(UNUSED1, UNUSED2)
+#endif
+
+#if GREATEST_USE_LONGJMP
+#define GREATEST_SAVE_CONTEXT() \
+ /* setjmp returns 0 (GREATEST_TEST_RES_PASS) on first call */ \
+ /* so the test runs, then RES_FAIL from FAIL_WITH_LONGJMP. */ \
+ ((enum greatest_test_res)(setjmp(greatest_info.jump_dest)))
+#else
+#define GREATEST_SAVE_CONTEXT() \
+ /*a no-op, since setjmp/longjmp aren't being used */ \
+ GREATEST_TEST_RES_PASS
+#endif
+
+/* Include several function definitions in the main test file. */
+#define GREATEST_MAIN_DEFS() \
+ \
+/* Is FILTER a subset of NAME? */ \
+static int greatest_name_match(const char *name, \
+ const char *filter) { \
+ size_t offset = 0; \
+ size_t filter_len = strlen(filter); \
+ while (name[offset] != '\0') { \
+ if (name[offset] == filter[0]) { \
+ if (0 == strncmp(&name[offset], filter, filter_len)) { \
+ return 1; \
+ } \
+ } \
+ offset++; \
+ } \
+ \
+ return 0; \
+} \
+ \
+int greatest_pre_test(const char *name) { \
+ if (!GREATEST_LIST_ONLY() \
+ && (!GREATEST_FIRST_FAIL() || greatest_info.suite.failed == 0) \
+ && (greatest_info.test_filter == NULL || \
+ greatest_name_match(name, greatest_info.test_filter))) { \
+ GREATEST_SET_TIME(greatest_info.suite.pre_test); \
+ if (greatest_info.setup) { \
+ greatest_info.setup(greatest_info.setup_udata); \
+ } \
+ return 1; /* test should be run */ \
+ } else { \
+ return 0; /* skipped */ \
+ } \
+} \
+ \
+void greatest_post_test(const char *name, int res) { \
+ GREATEST_SET_TIME(greatest_info.suite.post_test); \
+ if (greatest_info.teardown) { \
+ void *udata = greatest_info.teardown_udata; \
+ greatest_info.teardown(udata); \
+ } \
+ \
+ if (res <= GREATEST_TEST_RES_FAIL) { \
+ greatest_do_fail(name); \
+ } else if (res >= GREATEST_TEST_RES_SKIP) { \
+ greatest_do_skip(name); \
+ } else if (res == GREATEST_TEST_RES_PASS) { \
+ greatest_do_pass(name); \
+ } \
+ greatest_info.suite.tests_run++; \
+ greatest_info.col++; \
+ if (GREATEST_IS_VERBOSE()) { \
+ GREATEST_CLOCK_DIFF(greatest_info.suite.pre_test, \
+ greatest_info.suite.post_test); \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ } else if (greatest_info.col % greatest_info.width == 0) { \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ greatest_info.col = 0; \
+ } \
+ if (GREATEST_STDOUT == stdout) fflush(stdout); \
+} \
+ \
+static void report_suite(void) { \
+ if (greatest_info.suite.tests_run > 0) { \
+ fprintf(GREATEST_STDOUT, \
+ "\n%u test%s - %u passed, %u failed, %u skipped", \
+ greatest_info.suite.tests_run, \
+ greatest_info.suite.tests_run == 1 ? "" : "s", \
+ greatest_info.suite.passed, \
+ greatest_info.suite.failed, \
+ greatest_info.suite.skipped); \
+ GREATEST_CLOCK_DIFF(greatest_info.suite.pre_suite, \
+ greatest_info.suite.post_suite); \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ } \
+} \
+ \
+static void update_counts_and_reset_suite(void) { \
+ greatest_info.setup = NULL; \
+ greatest_info.setup_udata = NULL; \
+ greatest_info.teardown = NULL; \
+ greatest_info.teardown_udata = NULL; \
+ greatest_info.passed += greatest_info.suite.passed; \
+ greatest_info.failed += greatest_info.suite.failed; \
+ greatest_info.skipped += greatest_info.suite.skipped; \
+ greatest_info.tests_run += greatest_info.suite.tests_run; \
+ memset(&greatest_info.suite, 0, sizeof(greatest_info.suite)); \
+ greatest_info.col = 0; \
+} \
+ \
+static void greatest_run_suite(greatest_suite_cb *suite_cb, \
+ const char *suite_name) { \
+ if (greatest_info.suite_filter && \
+ !greatest_name_match(suite_name, greatest_info.suite_filter)) { \
+ return; \
+ } \
+ update_counts_and_reset_suite(); \
+ if (GREATEST_FIRST_FAIL() && greatest_info.failed > 0) { return; } \
+ fprintf(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name); \
+ GREATEST_SET_TIME(greatest_info.suite.pre_suite); \
+ suite_cb(); \
+ GREATEST_SET_TIME(greatest_info.suite.post_suite); \
+ report_suite(); \
+} \
+ \
+void greatest_do_pass(const char *name) { \
+ if (GREATEST_IS_VERBOSE()) { \
+ fprintf(GREATEST_STDOUT, "PASS %s: %s", \
+ name, greatest_info.msg ? greatest_info.msg : ""); \
+ } else { \
+ fprintf(GREATEST_STDOUT, "."); \
+ } \
+ greatest_info.suite.passed++; \
+} \
+ \
+void greatest_do_fail(const char *name) { \
+ if (GREATEST_IS_VERBOSE()) { \
+ fprintf(GREATEST_STDOUT, \
+ "FAIL %s: %s (%s:%u)", \
+ name, greatest_info.msg ? greatest_info.msg : "", \
+ greatest_info.fail_file, greatest_info.fail_line); \
+ } else { \
+ fprintf(GREATEST_STDOUT, "F"); \
+ greatest_info.col++; \
+ /* add linebreak if in line of '.'s */ \
+ if (greatest_info.col != 0) { \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ greatest_info.col = 0; \
+ } \
+ fprintf(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \
+ name, \
+ greatest_info.msg ? greatest_info.msg : "", \
+ greatest_info.fail_file, greatest_info.fail_line); \
+ } \
+ greatest_info.suite.failed++; \
+} \
+ \
+void greatest_do_skip(const char *name) { \
+ if (GREATEST_IS_VERBOSE()) { \
+ fprintf(GREATEST_STDOUT, "SKIP %s: %s", \
+ name, \
+ greatest_info.msg ? \
+ greatest_info.msg : "" ); \
+ } else { \
+ fprintf(GREATEST_STDOUT, "s"); \
+ } \
+ greatest_info.suite.skipped++; \
+} \
+ \
+int greatest_do_assert_equal_t(const void *exp, const void *got, \
+ greatest_type_info *type_info, void *udata) { \
+ int eq = 0; \
+ if (type_info == NULL || type_info->equal == NULL) { \
+ return 0; \
+ } \
+ eq = type_info->equal(exp, got, udata); \
+ if (!eq) { \
+ if (type_info->print != NULL) { \
+ fprintf(GREATEST_STDOUT, "\nExpected: "); \
+ (void)type_info->print(exp, udata); \
+ fprintf(GREATEST_STDOUT, "\n Got: "); \
+ (void)type_info->print(got, udata); \
+ fprintf(GREATEST_STDOUT, "\n"); \
+ } else { \
+ fprintf(GREATEST_STDOUT, \
+ "GREATEST_ASSERT_EQUAL_T failure at %s:%u\n", \
+ greatest_info.fail_file, \
+ greatest_info.fail_line); \
+ } \
+ } \
+ return eq; \
+} \
+ \
+void greatest_usage(const char *name) { \
+ fprintf(GREATEST_STDOUT, \
+ "Usage: %s [-hlfv] [-s SUITE] [-t TEST]\n" \
+ " -h, --help print this Help\n" \
+ " -l List suites and their tests, then exit\n" \
+ " -f Stop runner after first failure\n" \
+ " -v Verbose output\n" \
+ " -s SUITE only run suites containing string SUITE\n" \
+ " -t TEST only run tests containing string TEST\n", \
+ name); \
+} \
+ \
+static void greatest_parse_args(int argc, char **argv) { \
+ int i = 0; \
+ for (i = 1; i < argc; i++) { \
+ if (0 == strncmp("-t", argv[i], 2)) { \
+ if (argc <= i + 1) { \
+ greatest_usage(argv[0]); \
+ exit(EXIT_FAILURE); \
+ } \
+ greatest_info.test_filter = argv[i+1]; \
+ i++; \
+ } else if (0 == strncmp("-s", argv[i], 2)) { \
+ if (argc <= i + 1) { \
+ greatest_usage(argv[0]); \
+ exit(EXIT_FAILURE); \
+ } \
+ greatest_info.suite_filter = argv[i+1]; \
+ i++; \
+ } else if (0 == strncmp("-f", argv[i], 2)) { \
+ greatest_info.flags |= GREATEST_FLAG_FIRST_FAIL; \
+ } else if (0 == strncmp("-v", argv[i], 2)) { \
+ greatest_info.verbosity++; \
+ } else if (0 == strncmp("-l", argv[i], 2)) { \
+ greatest_info.flags |= GREATEST_FLAG_LIST_ONLY; \
+ } else if (0 == strncmp("-h", argv[i], 2) || \
+ 0 == strncmp("--help", argv[i], 6)) { \
+ greatest_usage(argv[0]); \
+ exit(EXIT_SUCCESS); \
+ } else if (0 == strncmp("--", argv[i], 2)) { \
+ break; \
+ } else { \
+ fprintf(GREATEST_STDOUT, \
+ "Unknown argument '%s'\n", argv[i]); \
+ greatest_usage(argv[0]); \
+ exit(EXIT_FAILURE); \
+ } \
+ } \
+} \
+ \
+int greatest_all_passed(void) { return (greatest_info.failed == 0); } \
+ \
+void greatest_set_test_filter(const char *name) { \
+ greatest_info.test_filter = name; \
+} \
+ \
+void greatest_set_suite_filter(const char *name) { \
+ greatest_info.suite_filter = name; \
+} \
+ \
+void greatest_get_report(struct greatest_report_t *report) { \
+ if (report) { \
+ report->passed = greatest_info.passed; \
+ report->failed = greatest_info.failed; \
+ report->skipped = greatest_info.skipped; \
+ report->assertions = greatest_info.assertions; \
+ } \
+} \
+ \
+unsigned int greatest_get_verbosity(void) { \
+ return greatest_info.verbosity; \
+} \
+ \
+void greatest_set_verbosity(unsigned int verbosity) { \
+ greatest_info.verbosity = (unsigned char)verbosity; \
+} \
+ \
+void greatest_set_flag(greatest_flag_t flag) { \
+ greatest_info.flags |= flag; \
+} \
+ \
+void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata) { \
+ greatest_info.setup = cb; \
+ greatest_info.setup_udata = udata; \
+} \
+ \
+void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, \
+ void *udata) { \
+ greatest_info.teardown = cb; \
+ greatest_info.teardown_udata = udata; \
+} \
+ \
+static int greatest_string_equal_cb(const void *exp, const void *got, \
+ void *udata) { \
+ size_t *size = (size_t *)udata; \
+ return (size != NULL \
+ ? (0 == strncmp((const char *)exp, (const char *)got, *size)) \
+ : (0 == strcmp((const char *)exp, (const char *)got))); \
+} \
+ \
+static int greatest_string_printf_cb(const void *t, void *udata) { \
+ (void)udata; /* note: does not check \0 termination. */ \
+ return fprintf(GREATEST_STDOUT, "%s", (const char *)t); \
+} \
+ \
+greatest_type_info greatest_type_info_string = { \
+ greatest_string_equal_cb, \
+ greatest_string_printf_cb, \
+}; \
+ \
+static int greatest_memory_equal_cb(const void *exp, const void *got, \
+ void *udata) { \
+ greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \
+ return (0 == memcmp(exp, got, env->size)); \
+} \
+ \
+static int greatest_memory_printf_cb(const void *t, void *udata) { \
+ greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \
+ unsigned char *buf = (unsigned char *)t, diff_mark = ' '; \
+ FILE *out = GREATEST_STDOUT; \
+ size_t i, line_i, line_len = 0; \
+ int len = 0; /* format hexdump with differences highlighted */ \
+ for (i = 0; i < env->size; i+= line_len) { \
+ diff_mark = ' '; \
+ line_len = env->size - i; \
+ if (line_len > 16) { line_len = 16; } \
+ for (line_i = i; line_i < i + line_len; line_i++) { \
+ if (env->exp[line_i] != env->got[line_i]) diff_mark = 'X'; \
+ } \
+ len += fprintf(out, "\n%04x %c ", (unsigned int)i, diff_mark); \
+ for (line_i = i; line_i < i + line_len; line_i++) { \
+ int m = env->exp[line_i] == env->got[line_i]; /* match? */ \
+ len += fprintf(out, "%02x%c", buf[line_i], m ? ' ' : '<'); \
+ } \
+ for (line_i = 0; line_i < 16 - line_len; line_i++) { \
+ len += fprintf(out, " "); \
+ } \
+ fprintf(out, " "); \
+ for (line_i = i; line_i < i + line_len; line_i++) { \
+ unsigned char c = buf[line_i]; \
+ len += fprintf(out, "%c", isprint(c) ? c : '.'); \
+ } \
+ } \
+ len += fprintf(out, "\n"); \
+ return len; \
+} \
+ \
+greatest_type_info greatest_type_info_memory = { \
+ greatest_memory_equal_cb, \
+ greatest_memory_printf_cb, \
+}; \
+ \
+greatest_run_info greatest_info
+
+/* Init internals. */
+#define GREATEST_INIT() \
+ do { \
+ /* Suppress unused function warning if features aren't used */ \
+ (void)greatest_run_suite; \
+ (void)greatest_parse_args; \
+ \
+ memset(&greatest_info, 0, sizeof(greatest_info)); \
+ greatest_info.width = GREATEST_DEFAULT_WIDTH; \
+ GREATEST_SET_TIME(greatest_info.begin); \
+ } while (0) \
+
+/* Handle command-line arguments, etc. */
+#define GREATEST_MAIN_BEGIN() \
+ do { \
+ GREATEST_INIT(); \
+ greatest_parse_args(argc, argv); \
+ } while (0)
+
+/* Report passes, failures, skipped tests, the number of
+ * assertions, and the overall run time. */
+#define GREATEST_PRINT_REPORT() \
+ do { \
+ if (!GREATEST_LIST_ONLY()) { \
+ update_counts_and_reset_suite(); \
+ GREATEST_SET_TIME(greatest_info.end); \
+ fprintf(GREATEST_STDOUT, \
+ "\nTotal: %u test%s", \
+ greatest_info.tests_run, \
+ greatest_info.tests_run == 1 ? "" : "s"); \
+ GREATEST_CLOCK_DIFF(greatest_info.begin, \
+ greatest_info.end); \
+ fprintf(GREATEST_STDOUT, ", %u assertion%s\n", \
+ greatest_info.assertions, \
+ greatest_info.assertions == 1 ? "" : "s"); \
+ fprintf(GREATEST_STDOUT, \
+ "Pass: %u, fail: %u, skip: %u.\n", \
+ greatest_info.passed, \
+ greatest_info.failed, greatest_info.skipped); \
+ } \
+ } while (0)
+
+/* Report results, exit with exit status based on results. */
+#define GREATEST_MAIN_END() \
+ do { \
+ GREATEST_PRINT_REPORT(); \
+ return (greatest_all_passed() ? EXIT_SUCCESS : EXIT_FAILURE); \
+ } while (0)
+
+/* Make abbreviations without the GREATEST_ prefix for the
+ * most commonly used symbols. */
+#if GREATEST_USE_ABBREVS
+#define TEST GREATEST_TEST
+#define SUITE GREATEST_SUITE
+#define SUITE_EXTERN GREATEST_SUITE_EXTERN
+#define RUN_TEST GREATEST_RUN_TEST
+#define RUN_TEST1 GREATEST_RUN_TEST1
+#define RUN_SUITE GREATEST_RUN_SUITE
+#define IGNORE_TEST GREATEST_IGNORE_TEST
+#define ASSERT GREATEST_ASSERT
+#define ASSERTm GREATEST_ASSERTm
+#define ASSERT_FALSE GREATEST_ASSERT_FALSE
+#define ASSERT_EQ GREATEST_ASSERT_EQ
+#define ASSERT_EQ_FMT GREATEST_ASSERT_EQ_FMT
+#define ASSERT_IN_RANGE GREATEST_ASSERT_IN_RANGE
+#define ASSERT_EQUAL_T GREATEST_ASSERT_EQUAL_T
+#define ASSERT_STR_EQ GREATEST_ASSERT_STR_EQ
+#define ASSERT_STRN_EQ GREATEST_ASSERT_STRN_EQ
+#define ASSERT_MEM_EQ GREATEST_ASSERT_MEM_EQ
+#define ASSERT_ENUM_EQ GREATEST_ASSERT_ENUM_EQ
+#define ASSERT_FALSEm GREATEST_ASSERT_FALSEm
+#define ASSERT_EQm GREATEST_ASSERT_EQm
+#define ASSERT_EQ_FMTm GREATEST_ASSERT_EQ_FMTm
+#define ASSERT_IN_RANGEm GREATEST_ASSERT_IN_RANGEm
+#define ASSERT_EQUAL_Tm GREATEST_ASSERT_EQUAL_Tm
+#define ASSERT_STR_EQm GREATEST_ASSERT_STR_EQm
+#define ASSERT_STRN_EQm GREATEST_ASSERT_STRN_EQm
+#define ASSERT_MEM_EQm GREATEST_ASSERT_MEM_EQm
+#define ASSERT_ENUM_EQm GREATEST_ASSERT_ENUM_EQm
+#define PASS GREATEST_PASS
+#define FAIL GREATEST_FAIL
+#define SKIP GREATEST_SKIP
+#define PASSm GREATEST_PASSm
+#define FAILm GREATEST_FAILm
+#define SKIPm GREATEST_SKIPm
+#define SET_SETUP GREATEST_SET_SETUP_CB
+#define SET_TEARDOWN GREATEST_SET_TEARDOWN_CB
+#define CHECK_CALL GREATEST_CHECK_CALL
+
+#ifdef GREATEST_VA_ARGS
+#define RUN_TESTp GREATEST_RUN_TESTp
+#endif
+
+#if GREATEST_USE_LONGJMP
+#define ASSERT_OR_LONGJMP GREATEST_ASSERT_OR_LONGJMP
+#define ASSERT_OR_LONGJMPm GREATEST_ASSERT_OR_LONGJMPm
+#define FAIL_WITH_LONGJMP GREATEST_FAIL_WITH_LONGJMP
+#define FAIL_WITH_LONGJMPm GREATEST_FAIL_WITH_LONGJMPm
+#endif
+
+#endif /* USE_ABBREVS */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+++ b/test/test_parg.c
@@ -1,0 +1,703 @@
+/*
+ * test_parg - parg unit test
+ *
+ * Written in 2015-2016 by Joergen Ibsen
+ *
+ * To the extent possible under law, the author(s) have dedicated all
+ * copyright and related and neighboring rights to this software to the
+ * public domain worldwide. This software is distributed without any
+ * warranty. <http://creativecommons.org/publicdomain/zero/1.0/>
+ */
+
+#include "parg.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "greatest.h"
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+static const char *os_def = ":no::r:";
+
+static const struct parg_option po_def[] = {
+ { "noarg", PARG_NOARG, NULL, 'n' },
+ { "optarg", PARG_OPTARG, NULL, 'o' },
+ { "reqarg", PARG_REQARG, NULL, 'r' },
+ { "foo", PARG_NOARG, NULL, 'f' },
+ { "foobar", PARG_NOARG, NULL, 'b' },
+ { 0, 0, 0, 0 }
+};
+
+/*
+ * parg_getopt tests
+ */
+
+TEST parg_getopt_app_only(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(1, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_dash(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ(1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(argv[1], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_double_dash(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_nonopt(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "foo" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ(1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(argv[1], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_no_match(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-u" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('?', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ('u', ps.optopt);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_noarg(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-n" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('n', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_optarg_missing(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-o" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('o', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_optarg_inline(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-oarg" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('o', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(&argv[1][2], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_reqarg_missing(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-r" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ(':', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ('r', ps.optopt);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_reqarg_inline(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-rarg" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('r', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(&argv[1][2], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_reqarg_nextarg(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-r", "arg" };
+
+ parg_init(&ps);
+
+ ASSERT_EQ('r', parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(argv[2], ps.optarg);
+ ASSERT_EQ(3, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt(&ps, ARRAY_SIZE(argv), argv, os_def));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(3, ps.optind);
+
+ PASS();
+}
+
+/*
+ * parg_getopt_long tests
+ */
+
+TEST parg_getopt_long_app_only(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(1, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_dash(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "-" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(argv[1], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_double_dash(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_nonopt(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "foo" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(argv[1], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_no_match(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--unknown" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('?', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_flag(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--flag" };
+ int li = -1;
+ int flag = 0;
+ struct parg_option po_flag[] = {
+ { "flag", PARG_NOARG, 0, 1 },
+ { 0, 0, 0, 0 }
+ };
+
+ po_flag[0].flag = &flag;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(0, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_flag, &li));
+ ASSERT_EQ(0, li);
+ ASSERT_EQ(1, flag);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_flag, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_noarg(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--noarg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('n', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(0, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_noarg_inline(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--noarg=arg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(':', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(0, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ('n', ps.optopt);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_optarg_missing(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--optarg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('o', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(1, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_optarg_inline(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--optarg=arg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('o', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(1, li);
+ ASSERT_EQ(&argv[1][9], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_reqarg_missing(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--reqarg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ(':', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(2, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ('r', ps.optopt);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_reqarg_inline(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--reqarg=arg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('r', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(2, li);
+ ASSERT_EQ(&argv[1][9], ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_reqarg_nextarg(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--reqarg", "arg" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('r', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(2, li);
+ ASSERT_EQ(argv[2], ps.optarg);
+ ASSERT_EQ(3, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(3, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_prefix_ambiguous(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--fo" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('?', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_prefix_exact(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--foo" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('f', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(3, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+TEST parg_getopt_long_prefix_unambiguous(void)
+{
+ struct parg_state ps;
+ char *argv[] = { "app", "--foob" };
+ int li = -1;
+
+ parg_init(&ps);
+
+ ASSERT_EQ('b', parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(4, li);
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ ASSERT_EQ(-1, parg_getopt_long(&ps, ARRAY_SIZE(argv), argv, ":", po_def, &li));
+ ASSERT_EQ(NULL, ps.optarg);
+ ASSERT_EQ(2, ps.optind);
+
+ PASS();
+}
+
+/*
+ * parg_reorder tests
+ */
+
+TEST parg_reorder_short(void)
+{
+ char *argv[] = {
+ "app", "first", "-r", "--", "foo", "-u", "bar", "-o", "--",
+ "-n", "last"
+ };
+ char *argv_expected[] = {
+ "app", "-r", "--", "-u", "-o", "--", "first", "foo", "bar",
+ "-n", "last"
+ };
+ size_t i;
+
+ ASSERT_EQ(6, parg_reorder(ARRAY_SIZE(argv), argv, os_def, NULL));
+
+ for (i = 0; i < ARRAY_SIZE(argv); ++i) {
+ ASSERT_STR_EQ(argv_expected[i], argv[i]);
+ }
+
+ PASS();
+}
+
+TEST parg_reorder_long(void)
+{
+ char *argv[] = {
+ "app", "first", "--reqarg", "--", "foo", "--unknown", "bar",
+ "--optarg", "--", "--noarg", "last"
+ };
+ char *argv_expected[] = {
+ "app", "--reqarg", "--", "--unknown", "--optarg", "--",
+ "first", "foo", "bar", "--noarg", "last"
+ };
+ size_t i;
+
+ ASSERT_EQ(6, parg_reorder(ARRAY_SIZE(argv), argv, "", po_def));
+
+ for (i = 0; i < ARRAY_SIZE(argv); ++i) {
+ ASSERT_STR_EQ(argv_expected[i], argv[i]);
+ }
+
+ PASS();
+}
+
+TEST parg_reorder_app_only(void)
+{
+ char *argv[] = { "app" };
+
+ ASSERT_EQ(1, parg_reorder(ARRAY_SIZE(argv), argv, os_def, NULL));
+
+ PASS();
+}
+
+TEST parg_reorder_double_dash_last(void)
+{
+ char *argv[] = { "app", "foo", "-n", "--" };
+ char *argv_expected[] = { "app", "-n", "--", "foo" };
+ size_t i;
+
+ ASSERT_EQ(3, parg_reorder(ARRAY_SIZE(argv), argv, os_def, NULL));
+
+ for (i = 0; i < ARRAY_SIZE(argv); ++i) {
+ ASSERT_STR_EQ(argv_expected[i], argv[i]);
+ }
+
+ PASS();
+}
+
+TEST parg_reorder_missing_arg_last(void)
+{
+ char *argv[] = { "app", "foo", "-r" };
+ char *argv_expected[] = { "app", "-r", "foo" };
+ size_t i;
+
+ ASSERT_EQ(2, parg_reorder(ARRAY_SIZE(argv), argv, os_def, NULL));
+
+ for (i = 0; i < ARRAY_SIZE(argv); ++i) {
+ ASSERT_STR_EQ(argv_expected[i], argv[i]);
+ }
+
+ PASS();
+}
+
+SUITE(parg_getopt_tests)
+{
+ RUN_TEST(parg_getopt_app_only);
+
+ RUN_TEST(parg_getopt_dash);
+
+ RUN_TEST(parg_getopt_double_dash);
+
+ RUN_TEST(parg_getopt_nonopt);
+
+ RUN_TEST(parg_getopt_no_match);
+
+ RUN_TEST(parg_getopt_noarg);
+
+ RUN_TEST(parg_getopt_optarg_missing);
+ RUN_TEST(parg_getopt_optarg_inline);
+
+ RUN_TEST(parg_getopt_reqarg_missing);
+ RUN_TEST(parg_getopt_reqarg_inline);
+ RUN_TEST(parg_getopt_reqarg_nextarg);
+}
+
+SUITE(parg_getopt_long_tests)
+{
+ RUN_TEST(parg_getopt_long_app_only);
+
+ RUN_TEST(parg_getopt_long_dash);
+
+ RUN_TEST(parg_getopt_long_double_dash);
+
+ RUN_TEST(parg_getopt_long_nonopt);
+
+ RUN_TEST(parg_getopt_long_no_match);
+
+ RUN_TEST(parg_getopt_long_flag);
+
+ RUN_TEST(parg_getopt_long_noarg);
+ RUN_TEST(parg_getopt_long_noarg_inline);
+
+ RUN_TEST(parg_getopt_long_optarg_missing);
+ RUN_TEST(parg_getopt_long_optarg_inline);
+
+ RUN_TEST(parg_getopt_long_reqarg_missing);
+ RUN_TEST(parg_getopt_long_reqarg_inline);
+ RUN_TEST(parg_getopt_long_reqarg_nextarg);
+
+ RUN_TEST(parg_getopt_long_prefix_ambiguous);
+ RUN_TEST(parg_getopt_long_prefix_exact);
+ RUN_TEST(parg_getopt_long_prefix_unambiguous);
+}
+
+SUITE(parg_reorder_tests)
+{
+ RUN_TEST(parg_reorder_short);
+ RUN_TEST(parg_reorder_long);
+
+ RUN_TEST(parg_reorder_app_only);
+
+ RUN_TEST(parg_reorder_double_dash_last);
+
+ RUN_TEST(parg_reorder_missing_arg_last);
+}
+
+GREATEST_MAIN_DEFS();
+
+int main(int argc, char *argv[])
+{
+ GREATEST_MAIN_BEGIN();
+ RUN_SUITE(parg_getopt_tests);
+ RUN_SUITE(parg_getopt_long_tests);
+ RUN_SUITE(parg_reorder_tests);
+ GREATEST_MAIN_END();
+}