tests: use -z / -n to test for string empty / non-empty
[babeltrace.git] / tests / plugins / sink.text.pretty / test_enum
index c00e6ac783670e70e514c2d497e00e9cc6e57132..723448b72ea85768ed828866f5a7704892869c29 100755 (executable)
@@ -8,7 +8,7 @@
 # not all covered by the main babeltrace tests with traces.
 SH_TAP=1
 
-if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
+if [ -n "${BT_TESTS_SRCDIR:-}" ]; then
        UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
 else
        UTILSSH="$(dirname "$0")/../../utils/utils.sh"
@@ -21,7 +21,7 @@ data_dir="$BT_TESTS_DATADIR/plugins/sink.text.pretty"
 temp_stdout_expected_file=$(mktemp -t test_pretty_expected_stdout.XXXXXX)
 temp_stderr_expected="/dev/null"
 
-plan_tests 14
+plan_tests 31
 
 function compare_enum_sorted
 {
@@ -36,7 +36,7 @@ function compare_enum_sorted
        # data structures differently (e.g. dictionaries are insertion sorted
        # since Python 3.7).
 
-       run_python_bt2 python3 "${BT_TESTS_SRCDIR}/utils/python/split_sort_compare.py" \
+       run_python_bt2 "${BT_TESTS_PYTHON_BIN}" "${BT_TESTS_SRCDIR}/utils/python/split_sort_compare.py" \
                "$(cat $expected_file)" "$(cat $actual_file)"
 }
 
@@ -57,6 +57,7 @@ function run_test
                "-p" "value=$value"
                "-p" "enum-signed=$enum_signed"
                "-c" "sink.text.pretty"
+               "-p" "print-enum-flags=true"
        )
 
        actual_stdout_file="$(mktemp -t actual_pretty_stdout.XXXXXX)"
@@ -101,6 +102,18 @@ function test_normal_enum {
        END
        run_test "$test_name" 0 7 "$temp_stdout_expected_file"
 
+       # Hit a range and a value
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( { "single3", "range" } : container = 4 ) }
+       END
+       run_test "$test_name" 0 4 "$temp_stdout_expected_file"
+
+       # Hit a range and a value
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( { "NOT_A_LABEL", "DOESNT_EXIST" } : container = 4 ) }
+       END
+       run_test "$test_name" 1 4 "$temp_stdout_expected_file"
+
        # Unknown
        cat <<- 'END' > "$temp_stdout_expected_file"
        with_enum: { enum_field = ( <unknown> : container = 21 ) }
@@ -150,10 +163,108 @@ function test_normal_enum_negative {
        run_test "$test_name" 0 0 "$temp_stdout_expected_file"
 }
 
+function test_bit_flag_enum {
+       test_name="Bit flag enum"
+       enum_signed="false"
+       enum_values="bit0,1,1 bit0bis,1,1 bit1,2,2 bit3,4,4 bit4,8,8 bit5,16,16 bit5,32,32"
+
+       # Single value hit
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "bit1" : container = 2 ) }
+       END
+       run_test "$test_name" 0 2 "$temp_stdout_expected_file"
+
+       # Multiple flags set
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "bit3" | "bit4" : container = 12 ) }
+       END
+       run_test "$test_name" 0 12 "$temp_stdout_expected_file"
+
+       # Some unknown bit
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( <unknown> : container = 68 ) }
+       END
+       run_test "$test_name" 0 68 "$temp_stdout_expected_file"
+
+       # Multiple labels for bit 0
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( { "bit0", "bit0bis" } : container = 1 ) }
+       END
+       run_test "$test_name" 0 1 "$temp_stdout_expected_file"
+
+       # Two labels for bit 0 and one label for bit 1
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( { "bit0", "bit0bis" } | "bit1" : container = 3 ) }
+       END
+       run_test "$test_name" 0 3 "$temp_stdout_expected_file"
+
+       # Single label for bit 0
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "bit5" | "bit5" : container = 48 ) }
+       END
+       run_test "$test_name" 0 48 "$temp_stdout_expected_file"
+
+       # negative value
+       enum_signed="true"
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( <unknown> : container = -1 ) }
+       END
+       run_test "$test_name" 0 -1 "$temp_stdout_expected_file"
+}
+
+function test_mixed_enum {
+       test_name="Mixed enum bits at beginning"
+       enum_signed="false"
+       enum_values="bit0,1,1 bit1,2,2 bit2,4,4 bit3,8,8 bit4,16,16 range,32,44 singleValue,45,45"
+
+       # Value with bit fields
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "bit0" | "bit1" | "bit2" | "bit3" | "bit4" : container = 31 ) }
+       END
+       run_test "$test_name" 0 31 "$temp_stdout_expected_file"
+
+       # A value with some bit flags set, but within another range
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "range" : container = 36 ) }
+       END
+       run_test "$test_name" 0 36 "$temp_stdout_expected_file"
+
+       # A value with some bit flags set, but corresponding to another value
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "singleValue" : container = 45 ) }
+       END
+       run_test "$test_name" 0 45 "$temp_stdout_expected_file"
+
+       # Value above the ranges
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( <unknown> : container = 46 ) }
+       END
+       run_test "$test_name" 0 46 "$temp_stdout_expected_file"
+
+       # Since low values are often powers of 2, they may be considered bit flags too
+       test_name="Mixed enum bits at end"
+       enum_signed="false"
+       enum_values="val1,1,1 val2,2,2 val3,3,3 val4,4,4 val5,5,5 bit3,8,8 bit4,16,16 bit5,32,32"
+
+       # Value with bit fields
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "bit4" : container = 16 ) }
+       END
+       run_test "$test_name" 0 16 "$temp_stdout_expected_file"
+
+       # Value with a bit field set both at beginning and end
+       cat <<- 'END' > "$temp_stdout_expected_file"
+       with_enum: { enum_field = ( "val1" | "bit4" : container = 17 ) }
+       END
+       run_test "$test_name" 0 17 "$temp_stdout_expected_file"
+}
+
 # Enumerations tests
 test_normal_enum "false"
 test_normal_enum "true"
 test_normal_enum_negative
+test_bit_flag_enum
+test_mixed_enum
 
 # Do not `rm` $temp_stderr_expected because it's set to `/dev/null` right now
 # and that would print an error.
This page took 0.025453 seconds and 4 git commands to generate.