#!/bin/bash

# Copyright (C) 2016, 2017, 2018, 2019  Stefan Vargyas
# 
# This file is part of Json-Type.
# 
# Json-Type is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Json-Type 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Json-Type.  If not, see <http://www.gnu.org/licenses/>.

#
# File generated by a command like:
# $ json-gentest -p litex -C gen-def:basic14
#

[[ "$1" =~ ^-u[0-9]+$ ]] &&
u="${1:2}" ||
u=""

diff -u$u -L basic14.old <(echo \
'$ [ -n "$JSON_LITEX_TEST_PCRE2_LIB" ] || JSON_LITEX_TEST_PCRE2_LIB="/usr/local/lib"
$ set -o pipefail
$ json-litex() {  LD_LIBRARY_PATH=../lib:"$JSON_LITEX_TEST_PCRE2_LIB" ../src/json --literal-value -VF -- ../lib/json-litex.so -D "$@"|sed -r '\''s/^#/\\#/;/^\s*\/\//d;/^\s*$/d;/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/{p;i'\''$'\''};\n'\'''\''};/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/,/^\}\s*;\s*$/d;/^\s+\.bytes\s*=\s*__0\s*,\s*$/!b;n;/^\s*\.size\s*=\s*[0-9]+\s*$/s/[0-9]+/0/'\''; }
$ json-litex -d '\''[{"baz":[{"bar":["`foo`"]}]}]'\''
static const struct json_litex_node_t __19;
static const struct json_litex_object_node_arg_t __15[];
static const struct json_litex_node_t __16;
static const struct json_litex_node_t __12;
static const struct json_litex_object_node_arg_t __8[];
static const struct json_litex_node_t __9;
static const struct json_litex_node_t __5;
static const struct json_litex_expr_node_t __0[] = {
    match_str("foo")
};
static const struct json_litex_expr_def_t __1 = {
    .nodes = __0,
    .size = 1
};
static const struct json_litex_node_t __2 = {
    .type = json_litex_string_node_type,
    .node.string = {
        .val = (const uchar_t*) "`foo`"
    },
    .attr.string = &__1,
    .parent = &__5
};
static const struct json_litex_array_attr_t __3 = {
    .string = &__2
};
static const struct json_litex_node_t* __4[] = {
    &__2,
    NULL
};
static const struct json_litex_node_t __5 = {
    .type = json_litex_array_node_type,
    .attr.array = &__3,
    .node.array = {
        .args = __4,
        .size = 1
    },
    .path = __8 + 0,
    .parent = &__9
};
static inline bool equal(
    const char* p, const char* q)
{
    while (*p && *p == *q)
         ++ p, ++ q;
    return *p == *q;
}
static const struct json_litex_node_t* __6(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "bar"))
        return &__5;
    return NULL;
}
static const struct json_litex_object_attr_t __7 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __6
};
static const struct json_litex_object_node_arg_t __8[] = {
    {
        .key = {
            .val = (const uchar_t*) "bar"
        },
        .val = &__5
    }
};
static const struct json_litex_node_t __9 = {
    .type = json_litex_object_node_type,
    .attr.object = &__7,
    .node.object = {
        .args = __8,
        .size = 1
    },
    .parent = &__12
};
static const struct json_litex_array_attr_t __10 = {
    .object = &__9
};
static const struct json_litex_node_t* __11[] = {
    &__9,
    NULL
};
static const struct json_litex_node_t __12 = {
    .type = json_litex_array_node_type,
    .attr.array = &__10,
    .node.array = {
        .args = __11,
        .size = 1
    },
    .path = __15 + 0,
    .parent = &__16
};
static const struct json_litex_node_t* __13(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "baz"))
        return &__12;
    return NULL;
}
static const struct json_litex_object_attr_t __14 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __13
};
static const struct json_litex_object_node_arg_t __15[] = {
    {
        .key = {
            .val = (const uchar_t*) "baz"
        },
        .val = &__12
    }
};
static const struct json_litex_node_t __16 = {
    .type = json_litex_object_node_type,
    .attr.object = &__14,
    .node.object = {
        .args = __15,
        .size = 1
    },
    .parent = &__19
};
static const struct json_litex_array_attr_t __17 = {
    .object = &__16
};
static const struct json_litex_node_t* __18[] = {
    &__16,
    NULL
};
static const struct json_litex_node_t __19 = {
    .type = json_litex_array_node_type,
    .attr.array = &__17,
    .node.array = {
        .args = __18,
        .size = 1
    }
};
static const struct json_litex_def_t MODULE_LITEX_DEF = {
    .node = &__19
};
$ json-litex -d '\''[{"baz":[{"bar":["/foo/"]}]}]'\''
\#if 0
enum {
  I = SZ(1) << json_litex_expr_rex_opt_ignore_case,
  S = SZ(1) << json_litex_expr_rex_opt_dot_match_all,
  U = SZ(1) << json_litex_expr_rex_opt_no_utf8_check,
  X = SZ(1) << json_litex_expr_rex_opt_extended_pat,
};
\#endif
static const uchar_t __0[] = {
};
\#if 0
static const struct json_litex_expr_rex_t __1[] = {
    {
        .text = (const uchar_t*) "foo"
    }
};
\#endif
static const struct json_litex_expr_rex_def_t __2 = {
    .codes = {
        .type = json_litex_expr_rex_codes_streams,
        .code.streams = {
            .bytes = __0,
            .size = 0
        }
    },
\#if 0
    .elems = __1,
\#endif
    .size = 1
};
static const struct json_litex_node_t __22;
static const struct json_litex_object_node_arg_t __18[];
static const struct json_litex_node_t __19;
static const struct json_litex_node_t __15;
static const struct json_litex_object_node_arg_t __11[];
static const struct json_litex_node_t __12;
static const struct json_litex_node_t __8;
static const struct json_litex_expr_node_t __3[] = {
    match_rex(0)
};
static const struct json_litex_expr_def_t __4 = {
    .nodes = __3,
    .size = 1
};
static const struct json_litex_node_t __5 = {
    .type = json_litex_string_node_type,
    .node.string = {
        .val = (const uchar_t*) "/foo/"
    },
    .attr.string = &__4,
    .parent = &__8
};
static const struct json_litex_array_attr_t __6 = {
    .string = &__5
};
static const struct json_litex_node_t* __7[] = {
    &__5,
    NULL
};
static const struct json_litex_node_t __8 = {
    .type = json_litex_array_node_type,
    .attr.array = &__6,
    .node.array = {
        .args = __7,
        .size = 1
    },
    .path = __11 + 0,
    .parent = &__12
};
static inline bool equal(
    const char* p, const char* q)
{
    while (*p && *p == *q)
         ++ p, ++ q;
    return *p == *q;
}
static const struct json_litex_node_t* __9(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "bar"))
        return &__8;
    return NULL;
}
static const struct json_litex_object_attr_t __10 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __9
};
static const struct json_litex_object_node_arg_t __11[] = {
    {
        .key = {
            .val = (const uchar_t*) "bar"
        },
        .val = &__8
    }
};
static const struct json_litex_node_t __12 = {
    .type = json_litex_object_node_type,
    .attr.object = &__10,
    .node.object = {
        .args = __11,
        .size = 1
    },
    .parent = &__15
};
static const struct json_litex_array_attr_t __13 = {
    .object = &__12
};
static const struct json_litex_node_t* __14[] = {
    &__12,
    NULL
};
static const struct json_litex_node_t __15 = {
    .type = json_litex_array_node_type,
    .attr.array = &__13,
    .node.array = {
        .args = __14,
        .size = 1
    },
    .path = __18 + 0,
    .parent = &__19
};
static const struct json_litex_node_t* __16(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "baz"))
        return &__15;
    return NULL;
}
static const struct json_litex_object_attr_t __17 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __16
};
static const struct json_litex_object_node_arg_t __18[] = {
    {
        .key = {
            .val = (const uchar_t*) "baz"
        },
        .val = &__15
    }
};
static const struct json_litex_node_t __19 = {
    .type = json_litex_object_node_type,
    .attr.object = &__17,
    .node.object = {
        .args = __18,
        .size = 1
    },
    .parent = &__22
};
static const struct json_litex_array_attr_t __20 = {
    .object = &__19
};
static const struct json_litex_node_t* __21[] = {
    &__19,
    NULL
};
static const struct json_litex_node_t __22 = {
    .type = json_litex_array_node_type,
    .attr.array = &__20,
    .node.array = {
        .args = __21,
        .size = 1
    }
};
static const struct json_litex_def_t MODULE_LITEX_DEF = {
    .rexes = &__2,
    .node = &__22
};
$ json-litex -d '\''[{"baz":[{"bar":["'\''\'\'''\''foo'\''\'\'''\''"]}]}]'\''
\#if 0
enum {
  I = SZ(1) << json_litex_expr_rex_opt_ignore_case,
  S = SZ(1) << json_litex_expr_rex_opt_dot_match_all,
  U = SZ(1) << json_litex_expr_rex_opt_no_utf8_check,
  X = SZ(1) << json_litex_expr_rex_opt_extended_pat,
};
\#endif
static const uchar_t __0[] = {
};
\#if 0
static const struct json_litex_expr_rex_t __1[] = {
    {
        .text = (const uchar_t*) "foo"
    }
};
\#endif
static const struct json_litex_expr_rex_def_t __2 = {
    .codes = {
        .type = json_litex_expr_rex_codes_streams,
        .code.streams = {
            .bytes = __0,
            .size = 0
        }
    },
\#if 0
    .elems = __1,
\#endif
    .size = 1
};
static const struct json_litex_node_t __22;
static const struct json_litex_object_node_arg_t __18[];
static const struct json_litex_node_t __19;
static const struct json_litex_node_t __15;
static const struct json_litex_object_node_arg_t __11[];
static const struct json_litex_node_t __12;
static const struct json_litex_node_t __8;
static const struct json_litex_expr_node_t __3[] = {
    match_rex(0)
};
static const struct json_litex_expr_def_t __4 = {
    .nodes = __3,
    .size = 1
};
static const struct json_litex_node_t __5 = {
    .type = json_litex_string_node_type,
    .node.string = {
        .val = (const uchar_t*) "'\''foo'\''"
    },
    .attr.string = &__4,
    .parent = &__8
};
static const struct json_litex_array_attr_t __6 = {
    .string = &__5
};
static const struct json_litex_node_t* __7[] = {
    &__5,
    NULL
};
static const struct json_litex_node_t __8 = {
    .type = json_litex_array_node_type,
    .attr.array = &__6,
    .node.array = {
        .args = __7,
        .size = 1
    },
    .path = __11 + 0,
    .parent = &__12
};
static inline bool equal(
    const char* p, const char* q)
{
    while (*p && *p == *q)
         ++ p, ++ q;
    return *p == *q;
}
static const struct json_litex_node_t* __9(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "bar"))
        return &__8;
    return NULL;
}
static const struct json_litex_object_attr_t __10 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __9
};
static const struct json_litex_object_node_arg_t __11[] = {
    {
        .key = {
            .val = (const uchar_t*) "bar"
        },
        .val = &__8
    }
};
static const struct json_litex_node_t __12 = {
    .type = json_litex_object_node_type,
    .attr.object = &__10,
    .node.object = {
        .args = __11,
        .size = 1
    },
    .parent = &__15
};
static const struct json_litex_array_attr_t __13 = {
    .object = &__12
};
static const struct json_litex_node_t* __14[] = {
    &__12,
    NULL
};
static const struct json_litex_node_t __15 = {
    .type = json_litex_array_node_type,
    .attr.array = &__13,
    .node.array = {
        .args = __14,
        .size = 1
    },
    .path = __18 + 0,
    .parent = &__19
};
static const struct json_litex_node_t* __16(
    const struct json_litex_trie_node_t* n UNUSED,
    const char* p)
{
    if (equal(p, "baz"))
        return &__15;
    return NULL;
}
static const struct json_litex_object_attr_t __17 = {
    .lookup = (json_litex_object_attr_lookup_func_t) __16
};
static const struct json_litex_object_node_arg_t __18[] = {
    {
        .key = {
            .val = (const uchar_t*) "baz"
        },
        .val = &__15
    }
};
static const struct json_litex_node_t __19 = {
    .type = json_litex_object_node_type,
    .attr.object = &__17,
    .node.object = {
        .args = __18,
        .size = 1
    },
    .parent = &__22
};
static const struct json_litex_array_attr_t __20 = {
    .object = &__19
};
static const struct json_litex_node_t* __21[] = {
    &__19,
    NULL
};
static const struct json_litex_node_t __22 = {
    .type = json_litex_array_node_type,
    .attr.array = &__20,
    .node.array = {
        .args = __21,
        .size = 1
    }
};
static const struct json_litex_def_t MODULE_LITEX_DEF = {
    .rexes = &__2,
    .node = &__22
};'
) -L basic14.new <(
echo '$ [ -n "$JSON_LITEX_TEST_PCRE2_LIB" ] || JSON_LITEX_TEST_PCRE2_LIB="/usr/local/lib"'
[ -n "$JSON_LITEX_TEST_PCRE2_LIB" ] || JSON_LITEX_TEST_PCRE2_LIB="/usr/local/lib" 2>&1 ||
echo 'command failed: [ -n "$JSON_LITEX_TEST_PCRE2_LIB" ] || JSON_LITEX_TEST_PCRE2_LIB="/usr/local/lib"'

echo '$ set -o pipefail'
set -o pipefail 2>&1 ||
echo 'command failed: set -o pipefail'

echo '$ json-litex() {  LD_LIBRARY_PATH=../lib:"$JSON_LITEX_TEST_PCRE2_LIB" ../src/json --literal-value -VF -- ../lib/json-litex.so -D "$@"|sed -r '\''s/^#/\\#/;/^\s*\/\//d;/^\s*$/d;/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/{p;i'\''$'\''};\n'\'''\''};/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/,/^\}\s*;\s*$/d;/^\s+\.bytes\s*=\s*__0\s*,\s*$/!b;n;/^\s*\.size\s*=\s*[0-9]+\s*$/s/[0-9]+/0/'\''; }'
json-litex() {  LD_LIBRARY_PATH=../lib:"$JSON_LITEX_TEST_PCRE2_LIB" ../src/json --literal-value -VF -- ../lib/json-litex.so -D "$@"|sed -r 's/^#/\\#/;/^\s*\/\//d;/^\s*$/d;/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/{p;i'$'};\n''};/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/,/^\}\s*;\s*$/d;/^\s+\.bytes\s*=\s*__0\s*,\s*$/!b;n;/^\s*\.size\s*=\s*[0-9]+\s*$/s/[0-9]+/0/'; } 2>&1 ||
echo 'command failed: json-litex() {  LD_LIBRARY_PATH=../lib:"$JSON_LITEX_TEST_PCRE2_LIB" ../src/json --literal-value -VF -- ../lib/json-litex.so -D "$@"|sed -r '\''s/^#/\\#/;/^\s*\/\//d;/^\s*$/d;/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/{p;i'\''$'\''};\n'\'''\''};/^static\s+const\s+uchar_t\s+__0\s*\[\s*\]\s*=\s*\{\s*$/,/^\}\s*;\s*$/d;/^\s+\.bytes\s*=\s*__0\s*,\s*$/!b;n;/^\s*\.size\s*=\s*[0-9]+\s*$/s/[0-9]+/0/'\''; }'

echo '$ json-litex -d '\''[{"baz":[{"bar":["`foo`"]}]}]'\'''
json-litex -d '[{"baz":[{"bar":["`foo`"]}]}]' 2>&1 ||
echo 'command failed: json-litex -d '\''[{"baz":[{"bar":["`foo`"]}]}]'\'''

echo '$ json-litex -d '\''[{"baz":[{"bar":["/foo/"]}]}]'\'''
json-litex -d '[{"baz":[{"bar":["/foo/"]}]}]' 2>&1 ||
echo 'command failed: json-litex -d '\''[{"baz":[{"bar":["/foo/"]}]}]'\'''

echo '$ json-litex -d '\''[{"baz":[{"bar":["'\''\'\'''\''foo'\''\'\'''\''"]}]}]'\'''
json-litex -d '[{"baz":[{"bar":["'\''foo'\''"]}]}]' 2>&1 ||
echo 'command failed: json-litex -d '\''[{"baz":[{"bar":["'\''\'\'''\''foo'\''\'\'''\''"]}]}]'\'''
)

