Revision: 77519
http://sourceforge.net/p/brlcad/code/77519
Author: starseeker
Date: 2020-10-20 20:00:21 +0000 (Tue, 20 Oct 2020)
Log Message:
-----------
Rough in a way to test the validity/invalidity of a plot3 file, using logic
similar to that used by rt_uplot_to_vlist. Idea is to be able to detect when
plotting file output has gone wrong for regression testing.
Modified Paths:
--------------
brlcad/trunk/src/libbn/plot3.c
brlcad/trunk/src/libbn/tests/CMakeLists.txt
Added Paths:
-----------
brlcad/trunk/src/libbn/tests/plot3.c
Modified: brlcad/trunk/src/libbn/plot3.c
===================================================================
--- brlcad/trunk/src/libbn/plot3.c 2020-10-20 19:14:03 UTC (rev 77518)
+++ brlcad/trunk/src/libbn/plot3.c 2020-10-20 20:00:21 UTC (rev 77519)
@@ -759,6 +759,266 @@
pdv_3cont(fp, tip);
}
+
+/*
+ * Routines to validate a plot file
+ */
+
+static int
+read_short(FILE *fp, int cnt, int mode)
+{
+ if (mode == PL_OUTPUT_MODE_BINARY) {
+ for (int i = 0; i < cnt * 2; i++) {
+ if (getc(fp) == EOF)
+ return 1;
+ }
+ return 0;
+ }
+ if (mode == PL_OUTPUT_MODE_TEXT) {
+ int ret;
+ double val;
+ for (int i = 0; i < cnt; i++) {
+ ret = fscanf(fp, "%lf", &val);
+ if (ret != 1)
+ return 1;
+ }
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+read_ieee(FILE *fp, int cnt, int mode)
+{
+ int ret;
+ if (mode == PL_OUTPUT_MODE_BINARY) {
+ for (int i = 0; i < cnt; i++) {
+ char inbuf[SIZEOF_NETWORK_DOUBLE];
+ ret = fread(inbuf, SIZEOF_NETWORK_DOUBLE, 1, fp);
+ if (ret != 1)
+ return 1;
+ }
+ return 0;
+ }
+ if (mode == PL_OUTPUT_MODE_TEXT) {
+ double val;
+ for (int i = 0; i < cnt; i++) {
+ ret = fscanf(fp, "%lf", &val);
+ if (ret != 1)
+ return 1;
+ }
+ return 0;
+ }
+ return 1;
+}
+
+static int
+read_tstring(FILE *fp, int mode)
+{
+ int ret;
+ if (mode == PL_OUTPUT_MODE_BINARY) {
+ int str_done = 0;
+ int cc;
+ while (!feof(fp) && !str_done) {
+ cc = getc(fp);
+ if (cc == '\n')
+ str_done = 1;
+ }
+ return 0;
+ }
+ if (mode == PL_OUTPUT_MODE_TEXT) {
+ char carg[256];
+ ret = fscanf(fp, "%256s\n", &carg[0]);
+ if (ret != 1)
+ return 1;
+ return 0;
+ }
+ return 1;
+}
+
+int
+plot3_invalid(FILE *fp, int mode)
+{
+
+ /* Only two valid modes */
+ if (mode != PL_OUTPUT_MODE_BINARY && mode != PL_OUTPUT_MODE_TEXT) {
+ return 1;
+ }
+
+ /* A non-readable file isn't a valid file */
+ if (!fp) {
+ return 1;
+ }
+
+ int i = 0;
+ int c;
+ unsigned int tchar = 0;
+
+ while (!feof(fp) && (c=getc(fp)) != EOF) {
+ if (c < 'A' || c > 'z') {
+ return 1;
+ }
+ switch (c) {
+ case 'C':
+ // TCHAR, 3, "color"
+ if (mode == PL_OUTPUT_MODE_BINARY) {
+ for (i = 0; i < 3; i++) {
+ if (getc(fp) == EOF)
+ return 1;
+ }
+ }
+ if (mode == PL_OUTPUT_MODE_TEXT) {
+ i = fscanf(fp, "%u", &tchar);
+ if (i != 1)
+ return 1;
+ }
+ break;
+ case 'F':
+ // TNONE, 0, "flush"
+ break;
+ case 'L':
+ // TSHORT, 6, "3line"
+ if (read_short(fp, 6, mode))
+ return 1;
+ break;
+ case 'M':
+ // TSHORT, 3, "3move"
+ if (read_short(fp, 3, mode))
+ return 1;
+ break;
+ case 'N':
+ // TSHORT, 3, "3cont"
+ if (read_short(fp, 3, mode))
+ return 1;
+ break;
+ case 'O':
+ // TIEEE, 3, "d_3move"
+ if (read_ieee(fp, 3, mode))
+ return 1;
+ break;
+ case 'P':
+ // TSHORT, 3, "3point"
+ if (read_short(fp, 3, mode))
+ return 1;
+ break;
+ case 'Q':
+ // TIEEE, 3, "d_3cont"
+ if (read_ieee(fp, 3, mode))
+ return 1;
+ break;
+ case 'S':
+ // TSHORT, 6, "3space"
+ if (read_short(fp, 6, mode))
+ return 1;
+ break;
+ case 'V':
+ // TIEEE, 6, "d_3line"
+ if (read_ieee(fp, 6, mode))
+ return 1;
+ break;
+ case 'W':
+ // TIEEE, 6, "d_3space"
+ if (read_ieee(fp, 6, mode))
+ return 1;
+ break;
+ case 'X':
+ // TIEEE, 3, "d_3point"
+ if (read_ieee(fp, 3, mode))
+ return 1;
+ break;
+ case 'a':
+ // TSHORT, 6, "arc"
+ if (read_short(fp, 6, mode))
+ return 1;
+ break;
+ case 'c':
+ // TSHORT, 3, "circle"
+ if (read_short(fp, 3, mode))
+ return 1;
+ break;
+ case 'e':
+ // TNONE, 0, "erase"
+ break;
+ case 'f':
+ // TSTRING, 1, "linmod"
+ if (read_tstring(fp, mode))
+ return 1;
+ break;
+ case 'i':
+ // TIEEE, 3, "d_circle"
+ if (read_ieee(fp, 3, mode))
+ return 1;
+ break;
+ case 'l':
+ // TSHORT, 4, "line"
+ if (read_short(fp, 4, mode))
+ return 1;
+ break;
+ case 'm':
+ // TSHORT, 2, "move"
+ if (read_short(fp, 2, mode))
+ return 1;
+ break;
+ case 'n':
+ // TSHORT, 2, "cont"
+ if (read_short(fp, 2, mode))
+ return 1;
+ break;
+ case 'o':
+ // TIEEE, 2, "d_move"
+ if (read_ieee(fp, 2, mode))
+ return 1;
+ break;
+ case 'p':
+ // TSHORT, 2, "point"
+ if (read_short(fp, 2, mode))
+ return 1;
+ break;
+ case 'q':
+ // TIEEE, 2, "d_cont"
+ if (read_ieee(fp, 2, mode))
+ return 1;
+ break;
+ case 'r':
+ // TIEEE, 6, "d_arc"
+ if (read_ieee(fp, 6, mode))
+ return 1;
+ break;
+ case 's':
+ // TSHORT, 4, "space"
+ if (read_short(fp, 4, mode))
+ return 1;
+ break;
+ case 't':
+ // TSTRING, 1, "label"
+ if (read_tstring(fp, mode))
+ return 1;
+ break;
+ case 'v':
+ // TIEEE, 4, "d_line"
+ if (read_ieee(fp, 4, mode))
+ return 1;
+ break;
+ case 'w':
+ // TIEEE, 4, "d_space"
+ if (read_ieee(fp, 4, mode))
+ return 1;
+ break;
+ case 'x':
+ // TIEEE, 2, "d_point"
+ if (read_ieee(fp, 2, mode))
+ return 1;
+ break;
+ default:
+ return 1;
+ break;
+ };
+ }
+
+ return 0;
+}
+
/** @} */
/*
* Local Variables:
Modified: brlcad/trunk/src/libbn/tests/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbn/tests/CMakeLists.txt 2020-10-20 19:14:03 UTC (rev
77518)
+++ brlcad/trunk/src/libbn/tests/CMakeLists.txt 2020-10-20 20:00:21 UTC (rev
77519)
@@ -48,6 +48,11 @@
# WIP - needs to become real test
BRLCAD_ADDEXEC(bn_randsph randsph.c "libbu;libbn" TEST)
+# This test program is being kept separate because the plot3 logic really
+# should move, and when it does it will be easier to move the test if it
+# is not tied into the bn_test target
+BRLCAD_ADDEXEC(bn_plot3 plot3.c "libbu;libbn" TEST)
+
#
# ************ list.c tests *************
#
Added: brlcad/trunk/src/libbn/tests/plot3.c
===================================================================
--- brlcad/trunk/src/libbn/tests/plot3.c (rev 0)
+++ brlcad/trunk/src/libbn/tests/plot3.c 2020-10-20 20:00:21 UTC (rev
77519)
@@ -0,0 +1,97 @@
+/* P L O T 3 . C
+ * BRL-CAD
+ *
+ * Copyright (c) 2014-2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+
+#include "common.h"
+
+#include <stdio.h>
+
+#include "bu/app.h"
+#include "bu/malloc.h"
+#include "bu/log.h"
+#include "vmath.h"
+#include "bu/file.h"
+#include "bu/opt.h"
+#include "bn/plot3.h"
+
+int
+main(int argc, const char *argv[])
+{
+ int ret = 0;
+ int print_help = 0;
+ int binary_mode = 0;
+ int text_mode = 0;
+ int mode = -1;
+ struct bu_opt_desc d[4];
+ BU_OPT(d[0], "h", "help", "", NULL, &print_help, "Print help and
exit");
+ BU_OPT(d[1], "b", "binary", "", NULL, &binary_mode, "Process plot file as
binary plot data (default)");
+ BU_OPT(d[2], "t", "text", "", NULL, &text_mode, "Process plot file as
text plot data");
+ BU_OPT_NULL(d[3]);
+
+ bu_setprogname(argv[0]);
+
+ argc-=(argc>0); argv+=(argc>0); /* done with command name argv[0] */
+
+ int opt_ret = bu_opt_parse(NULL, argc, argv, d);
+
+ if (!opt_ret)
+ bu_exit(1, "Usage: %s [opts] file\n", argv[0]);
+
+ if (binary_mode && text_mode)
+ bu_exit(1, "Error - specify either binary mode or text mode\n");
+
+
+ if (binary_mode)
+ mode = PL_OUTPUT_MODE_BINARY;
+
+ if (text_mode)
+ mode = PL_OUTPUT_MODE_BINARY;
+
+ if (!bu_file_exists(argv[0], NULL)) {
+ bu_exit(1, "file %s not found\n", argv[0]);
+ }
+
+ FILE *fp = fopen(argv[0], "rb");
+
+ /* A non-readable file isn't a valid file */
+ if (!fp)
+ bu_exit(1, "Error - could not open file %s\n", argv[0]);
+
+ ret = plot3_invalid(fp, mode);
+
+ if (ret) {
+ bu_log("INVALID: %s\n", argv[0]);
+ } else {
+ bu_log("VALID: %s\n", argv[0]);
+ }
+
+ return ret;
+}
+
+
+/** @} */
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
Property changes on: brlcad/trunk/src/libbn/tests/plot3.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits