[esnacc-dev] [PATCH 5/5] cxx-examples: add automatic tags example

Aaron Conole aconole at bytheb.org
Fri Jul 15 16:07:52 UTC 2016


This commit introduces an automatic tagging example, along with some
simple test vectors.

Signed-off-by: Aaron Conole <aconole at bytheb.org>
---
 cxx-examples/.gitignore        |   2 +
 cxx-examples/automake.mk       |  11 ++++
 cxx-examples/src/automatic.cpp | 125 +++++++++++++++++++++++++++++++++++++++++
 cxx-examples/src/autotags.asn1 |  11 ++++
 cxx-examples/src/main.cpp      |   4 ++
 5 files changed, 153 insertions(+)
 create mode 100644 cxx-examples/src/automatic.cpp
 create mode 100644 cxx-examples/src/autotags.asn1

diff --git a/cxx-examples/.gitignore b/cxx-examples/.gitignore
index 9a5acd0..13f9e58 100644
--- a/cxx-examples/.gitignore
+++ b/cxx-examples/.gitignore
@@ -4,6 +4,8 @@
 *.log
 *.trs
 main
+src/autotags.cpp
+src/autotags.h
 src/rfc1157-snmp.cpp
 src/rfc1157-snmp.h
 src/rfc1155-smi.cpp
diff --git a/cxx-examples/automake.mk b/cxx-examples/automake.mk
index 2e927f5..69dafcf 100644
--- a/cxx-examples/automake.mk
+++ b/cxx-examples/automake.mk
@@ -5,6 +5,8 @@ check_PROGRAMS += cxx-examples/main
 cxx_examples_main_SOURCES = \
 	cxx-examples/src/asnutil.cpp \
 	cxx-examples/src/asnutil.h \
+	cxx-examples/src/autotags.cpp \
+	cxx-examples/src/automatic.cpp \
 	cxx-examples/src/bitstest.cpp \
 	cxx-examples/src/gfsi.cpp \
 	cxx-examples/src/inttest.cpp \
@@ -44,12 +46,21 @@ $(top_builddir)/cxx-examples/src/rfc1157-snmp.cpp: \
 	$(top_builddir)/compiler/esnacc -C -mo `dirname $@` \
 	$< -I $(top_srcdir)/cxx-examples/src
 
+$(top_builddir)/cxx-examples/src/autotags.cpp: \
+ $(top_srcdir)/cxx-examples/src/autotags.asn1 \
+ $(top_builddir)/compiler/esnacc
+	$(top_builddir)/compiler/esnacc -C -mo `dirname $@` \
+	$<
+
 EXTRA_DIST+= \
+	cxx-examples/src/autotags.asn1 \
 	cxx-examples/src/rfc1155-smi.asn1 \
 	cxx-examples/src/rfc1157-snmp.asn1
 
 CLEANFILES+= \
 	test.txt \
+	cxx-examples/src/autotags.cpp \
+	cxx-examples/src/autotags.h \
 	cxx-examples/src/rfc1157-snmp.cpp \
 	cxx-examples/src/rfc1157-snmp.h \
 	cxx-examples/src/rfc1155-smi.cpp \
diff --git a/cxx-examples/src/automatic.cpp b/cxx-examples/src/automatic.cpp
new file mode 100644
index 0000000..8f57d23
--- /dev/null
+++ b/cxx-examples/src/automatic.cpp
@@ -0,0 +1,125 @@
+#include "autotags.h"
+
+static SNACC::Human *
+getHuman(const char *name, int age, bool isBiblical,
+         const char *firstWords = NULL)
+{
+    SNACC::Human *h = new SNACC::Human();
+    h->name = name;
+    if (firstWords)
+        h->first_words = new SNACC::UTF8String(firstWords);
+    h->age = new SNACC::HumanChoice();
+    if (isBiblical) {
+        h->age->biblical = new SNACC::HumanChoice::Biblical(age);
+        h->age->choiceId = SNACC::HumanChoice::biblicalCid;
+    } else {
+        h->age->modern = new SNACC::HumanChoice::Modern(age);
+        h->age->choiceId = SNACC::HumanChoice::modernCid;
+    }
+    return h;
+}
+
+int automaticTests()
+{
+    struct Tests {
+        const char bytes[256];
+        size_t byte_len;
+        const char *enc_bytes;
+        SNACC::Human *h;
+    } t[] = {
+        {
+            {0x30,0x0B,0x80,0x04,0x41,0x64,0x61,0x6D,0xA2,0x03,0x81,0x01,0x5A,},
+            13,
+            NULL,
+            getHuman("Adam", 90, false)
+        },
+        {
+            {0x30,0x0C,0x80,0x04,0x41,0x64,0x61,0x6D,0xA2,0x04,0x80,0x02,0x03,
+             0x84,},
+            14,
+            NULL,
+            getHuman("Adam", 900, true)
+        },
+        {
+            {0x30,0x1A,0x80,0x04,0x41,0x64,0x61,0x6D,0x81,0x0D,0x67,0x6F,0x6F,
+             0x20,0x67,0x6F,0x6F,0x20,0x67,0x61,0x20,0x67,0x61,0xA2,0x03,0x81,
+             0x01,0x5A,},
+            28,
+            NULL,
+            getHuman("Adam", 90, false, "goo goo ga ga")
+        },
+        {
+            {0x30,0x1B,0x80,0x04,0x41,0x64,0x61,0x6D,0x81,0x0D,0x67,0x6F,0x6F,
+             0x20,0x67,0x6F,0x6F,0x20,0x67,0x61,0x20,0x67,0x61,0xA2,0x04,0x80,
+             0x02,0x03,0x84,},
+            29,
+            NULL,
+            getHuman("Adam", 900, true, "goo goo ga ga")
+        },
+    };
+
+    size_t i;
+
+    for(i = 0; i < (sizeof(t) / sizeof(t[0])); i++) {
+        std::cout << "T[" << i << "]: Starting HUMAN test" << std::endl;
+        bool fail_dec = false, fail_enc = false;
+        bool fail_name = false;
+        bool fail_age = false;
+        bool fail_firstwords = false;
+        SNACC::AsnBuf b(t[i].bytes, t[i].byte_len);
+        SNACC::AsnLen l;
+        SNACC::Human h;
+        try {
+            h.BDec(b, l);
+            if (h.name != t[i].h->name) {
+                fail_name = true;
+            }
+
+            if (h.age->choiceId != t[i].h->age->choiceId) {
+                fail_age = true;
+            } else {
+                if (h.age->choiceId == SNACC::HumanChoice::biblicalCid) {
+                    if (*(h.age->biblical) != *(t[i].h->age->biblical)) {
+                        fail_age = true;
+                    }
+                } else {
+                    if (*(h.age->modern) != *(t[i].h->age->modern)) {
+                        fail_age = true;
+                    }
+                }
+            }
+            
+        } catch (SNACC::SnaccException &e) {
+            fail_dec = true;
+        }
+
+        if (fail_dec || fail_name || fail_age || fail_firstwords) {
+            std::cout << "D:F(" << i << "): " << fail_dec << ","
+                      << fail_name << ","
+                      << fail_age << ","
+                      << fail_firstwords
+                      << std::endl;
+            return 1;
+        }
+
+        SNACC::AsnBuf benc;
+        SNACC::AsnBuf expected(t[i].bytes, t[i].byte_len);
+        try {
+            h.BEnc(benc);
+            if (!(benc == expected)) {
+                fail_enc = true;
+            }
+        } catch (SNACC::SnaccException &e) {
+            fail_enc = true;
+        }
+        if (fail_enc || fail_name || fail_age || fail_firstwords) {
+            std::cout << "E:F(" << i << "): " << fail_enc << ","
+                      << fail_name << ","
+                      << fail_age << ","
+                      << fail_firstwords
+                      << std::endl;
+            return 1;
+        }
+    }
+    return 0;
+}
diff --git a/cxx-examples/src/autotags.asn1 b/cxx-examples/src/autotags.asn1
new file mode 100644
index 0000000..8add311
--- /dev/null
+++ b/cxx-examples/src/autotags.asn1
@@ -0,0 +1,11 @@
+World-Schema DEFINITIONS AUTOMATIC TAGS ::= 
+BEGIN
+  Human ::= SEQUENCE {
+     name UTF8String,
+     first-words UTF8String DEFAULT "Hello World",
+     age  CHOICE {
+        biblical INTEGER (1..1000),
+        modern INTEGER (1..100)
+     } OPTIONAL
+  }
+END
diff --git a/cxx-examples/src/main.cpp b/cxx-examples/src/main.cpp
index 55f20fe..30e1680 100644
--- a/cxx-examples/src/main.cpp
+++ b/cxx-examples/src/main.cpp
@@ -6,6 +6,7 @@
 #endif
 
 void newBufTest();
+int automaticTests();
 
 using namespace SNACC;
 
@@ -180,6 +181,9 @@ int main(int argc, char *argv[])
        }
 
        fillTest();
+       if (automaticTests()) {
+           std::cout << "Automatic test failure." << std::endl;
+       }
    } catch (SnaccException &e) {
        std::cout << "\n**** Caught SnaccException ****\n";
        std::cout << "What: " << e.what() << std::endl;
-- 
2.5.5




More information about the dev mailing list