aboutsummaryrefslogtreecommitdiffstats
path: root/tests/extensions/encode_extensions.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/extensions/encode_extensions.c')
-rw-r--r--tests/extensions/encode_extensions.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/tests/extensions/encode_extensions.c b/tests/extensions/encode_extensions.c
index 8857f14..dee3597 100644
--- a/tests/extensions/encode_extensions.c
+++ b/tests/extensions/encode_extensions.c
@@ -7,25 +7,37 @@
#include <pb_encode.h>
#include "alltypes.pb.h"
#include "extensions.pb.h"
+#include "test_helpers.h"
int main(int argc, char **argv)
{
- AllTypes alltypes = {};
+ uint8_t buffer[1024];
+ pb_ostream_t stream;
+ AllTypes alltypes = {0};
int32_t extensionfield1 = 12345;
- pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
+ pb_extension_t ext1;
+ ExtensionMessage extensionfield2 = {"test", 54321};
+ pb_extension_t ext2;
+
+ /* Set up the extensions */
alltypes.extensions = &ext1;
- ExtensionMessage extensionfield2 = {"test", 54321};
- pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
+ ext1.type = &AllTypes_extensionfield1;
+ ext1.dest = &extensionfield1;
ext1.next = &ext2;
- uint8_t buffer[1024];
- pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
+ ext2.dest = &extensionfield2;
+ ext2.next = NULL;
+
+ /* Set up the output stream */
+ stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
- /* Now encode it and check if we succeeded. */
+ /* Now encode the message and check if we succeeded. */
if (pb_encode(&stream, AllTypes_fields, &alltypes))
{
+ SET_BINARY_MODE(stdout);
fwrite(buffer, 1, stream.bytes_written, stdout);
return 0; /* Success */
}
99 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
Help('''
Type 'scons' to build and run all the available test cases.
It will automatically detect your platform and C compiler and
build appropriately.

You can modify the behavious using following options:
CC          Name of C compiler
CXX         Name of C++ compiler
CCFLAGS     Flags to pass to the C compiler
CXXFLAGS    Flags to pass to the C++ compiler

For example, for a clang build, use:
scons CC=clang CXX=clang++
''')

import os
env = Environment(ENV = os.environ)

# Allow overriding the compiler with scons CC=???
if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX'])
if 'CFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CFLAGS'])
if 'CXXFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CXXFLAGS'])

# Add the builders defined in site_init.py
add_nanopb_builders(env)

# Path to the files shared by tests, and to the nanopb core.
env.Append(CPPPATH = ["#../", "#common"])

# Path for finding nanopb.proto
env.Append(PROTOCPATH = '#../generator')

# Check the compilation environment, unless we are just cleaning up.
if not env.GetOption('clean'):
    conf = Configure(env)

    # If the platform doesn't support C99, use our own header file instead.
    stdbool = conf.CheckCHeader('stdbool.h')
    stdint = conf.CheckCHeader('stdint.h')
    stddef = conf.CheckCHeader('stddef.h')
    string = conf.CheckCHeader('string.h')
    if not stdbool or not stdint or not stddef or not string:
        conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
        conf.env.Append(CPPPATH = "#../compat")
        
        if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
        if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
        if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1})
        if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1})
        
    # Check if we can use pkg-config to find protobuf include path
    status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET')
    if status:
        conf.env.Append(PROTOCPATH = output.strip())
    else:
        conf.env.Append(PROTOCPATH = '/usr/include')
    
    # Check if libmudflap is available (only with GCC)
    if 'gcc' in env['CC']:
        if conf.CheckLib('mudflap'):
            conf.env.Append(CCFLAGS = '-fmudflap')
            conf.env.Append(LINKFLAGS = '-lmudflap -fmudflap')
    
    # End the config stuff
    env = conf.Finish()

# Initialize the CCFLAGS according to the compiler
if 'gcc' in env['CC']:
    # GNU Compiler Collection
    
    # Debug info, warnings as errors
    env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror --coverage -fstack-protector-all')
    env.Append(LINKFLAGS = '--coverage')
    
    # More strict checks on the nanopb core
    env.Append(CORECFLAGS = '-Wextra -Wcast-qual -Wlogical-op -Wconversion')
elif 'clang' in env['CC']:
    # CLang
    env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror')
    env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
elif 'cl' in env['CC']:
    # Microsoft Visual C++
    
    # Debug info on, warning level 2 for tests, warnings as errors
    env.Append(CFLAGS = '/Zi /W2 /WX')
    env.Append(LINKFLAGS = '/DEBUG')
    
    # More strict checks on the nanopb core
    env.Append(CORECFLAGS = '/W4')
    
    # PB_RETURN_ERROR triggers C4127 because of while(0)
    env.Append(CFLAGS = '/wd4127')
elif 'tcc' in env['CC']:
    # Tiny C Compiler
    env.Append(CFLAGS = '-Wall -Werror -g')

env.SetDefault(CORECFLAGS = '')

if 'clang++' in env['CXX']:
    env.Append(CXXFLAGS = '-g -O0 -Wall -Werror -Wextra -Wno-missing-field-initializers')
elif 'g++' in env['CXX']:
    env.Append(CXXFLAGS = '-g -O0 -Wall -Werror -Wextra -Wno-missing-field-initializers')
elif 'cl' in env['CXX']:
    env.Append(CXXFLAGS = '/Zi /W2 /WX')
    
# Now include the SConscript files from all subdirectories
SConscript(Glob('*/SConscript'), exports = 'env')