summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ff53c1c)
raw | patch | inline | side by side (parent: ff53c1c)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 8 Oct 2014 10:24:51 +0000 (12:24 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 8 Oct 2014 10:24:51 +0000 (12:24 +0200) |
This function parses the string representation of an operator.
src/core/data.c | patch | blob | history | |
src/include/core/data.h | patch | blob | history | |
t/unit/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 2a8c439e08c7da72dca03ce9b28e0c59413e36b2..4e52133484b8e219cdef562a86a7a9991bc2279e 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
return 0;
} /* sdb_data_isnull */
+int
+sdb_data_parse_op(const char *op)
+{
+ if (! strcmp(op, "+"))
+ return SDB_DATA_ADD;
+ else if (! strcmp(op, "-"))
+ return SDB_DATA_SUB;
+ else if (! strcmp(op, "*"))
+ return SDB_DATA_MUL;
+ else if (! strcmp(op, "/"))
+ return SDB_DATA_DIV;
+ else if (! strcmp(op, "%"))
+ return SDB_DATA_MOD;
+ else if (! strcmp(op, "||"))
+ return SDB_DATA_CONCAT;
+ return -1;
+} /* sdb_data_parse_op */
+
int
sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
sdb_data_t *res)
index 87607f810d344fc87d7bbc5943d16caaaa7fe9d7..f1c0324a1e6b7cd9d736255ec25917b3bfa2325f 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
};
#define SDB_DATA_OP_TO_STRING(op) \
- (((op) == SDB_DATA_ADD) \
- ? "+" \
- : ((op) == SDB_DATA_SUB) \
- ? "-" \
- : ((op) == SDB_DATA_MUL) \
- ? "*" \
- : ((op) == SDB_DATA_DIV) \
- ? "/" \
- : ((op) == SDB_DATA_MOD) \
- ? "%" \
- : ((op) == SDB_DATA_CONCAT) \
- ? "||" : "UNKNOWN")
+ (((op) == SDB_DATA_ADD) ? "+" \
+ : ((op) == SDB_DATA_SUB) ? "-" \
+ : ((op) == SDB_DATA_MUL) ? "*" \
+ : ((op) == SDB_DATA_DIV) ? "/" \
+ : ((op) == SDB_DATA_MOD) ? "%" \
+ : ((op) == SDB_DATA_CONCAT) ? "||" : "UNKNOWN")
+
+/*
+ * sdb_data_parse_op:
+ * Parse the string representation of an operator supported by
+ * sdb_data_expr_eval.
+ *
+ * Returns:
+ * - the ID of the operator
+ * - a negative value in case the operator does not exist
+ */
+int
+sdb_data_parse_op(const char *op);
/*
* sdb_data_expr_eval:
index 742466eec0008c599824de8c49bb37aacefc3d65..a1b4caa59965c7b307dcf848d40f0011ea4ea1b8 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
}
END_TEST
+START_TEST(test_parse_op)
+{
+ struct {
+ const char *op;
+ int id;
+ } golden_data[] = {
+ { "+", SDB_DATA_ADD },
+ { "-", SDB_DATA_SUB },
+ { "*", SDB_DATA_MUL },
+ { "/", SDB_DATA_DIV },
+ { "%", SDB_DATA_MOD },
+ { "||", SDB_DATA_CONCAT },
+ { "&&", -1 },
+ };
+
+ size_t i;
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ const char *op;
+ int id;
+
+ id = sdb_data_parse_op(golden_data[i].op);
+ fail_unless(id == golden_data[i].id,
+ "sdb_data_parse_op(%s) = %d; expected: %d",
+ golden_data[i].op, id, golden_data[i].id);
+
+ if (id <= 0)
+ continue;
+
+ op = SDB_DATA_OP_TO_STRING(id);
+ fail_unless(!strcmp(op, golden_data[i].op),
+ "SDB_DATA_OP_TO_STRING(%d) = '%s'; expected: '%s'",
+ id, op, golden_data[i].op);
+ }
+}
+END_TEST
+
START_TEST(test_expr_eval)
{
struct {
tcase_add_test(tc, test_data);
tcase_add_test(tc, test_cmp);
tcase_add_test(tc, test_strcmp);
+ tcase_add_test(tc, test_parse_op);
tcase_add_test(tc, test_expr_eval);
tcase_add_test(tc, test_format);
tcase_add_test(tc, test_parse);