*/
#include <tensor_common.h>
+#include <string.h>
+#include <glib.h>
/**
* @brief String representations for each tensor element type.
[_NNS_UINT8] = "uint8",
[_NNS_FLOAT64] = "float64",
[_NNS_FLOAT32] = "float32",
+ [_NNS_END] = NULL,
};
+
+
+/**
+ * @brief Get tensor_type from string tensor_type input
+ * @return Corresponding tensor_type. _NNS_END if unrecognized value is there.
+ * @param typestr The string type name, supposed to be one of tensor_element_typename[]
+ */
+tensor_type get_tensor_type(const gchar* typestr) {
+ int len;
+
+ if (!typestr)
+ return _NNS_END;
+ len = strlen(typestr);
+
+ if (typestr[0] == 'u' || typestr[0] == 'U') {
+ /* Let's believe the developer and the following three letters are "int" (case insensitive) */
+ if (len == 6) /* uint16, uint32 */ {
+ if (typestr[4] == '1' && typestr[5] == '6')
+ return _NNS_UINT16;
+ else if (typestr[4] == '3' && typestr[5] == '2')
+ return _NNS_UINT32;
+ } else if (len == 5) /* uint8 */ {
+ if (typestr[4] == '8')
+ return _NNS_UINT8;
+ }
+ } else if (typestr[0] == 'i' || typestr[0] == 'I') {
+ /* Let's believe the developer and the following two letters are "nt" (case insensitive) */
+ if (len == 5) /* int16, int32 */ {
+ if (typestr[3] == '1' && typestr[4] == '6')
+ return _NNS_INT16;
+ else if (typestr[3] == '3' && typestr[4] == '2')
+ return _NNS_INT32;
+ } else if (len == 4) /* int8 */ {
+ if (typestr[3] == '8')
+ return _NNS_INT8;
+ }
+ return _NNS_END;
+ } else if (typestr[0] == 'f' || typestr[0] == 'F') {
+ /* Let's assume that the following 4 letters are "loat" */
+ if (len == 7) {
+ if (typestr[5] == '6' && typestr[6] == '4')
+ return _NNS_FLOAT64;
+ else if (typestr[5] == '3' && typestr[6] == '2')
+ return _NNS_FLOAT32;
+ }
+ }
+
+ return _NNS_END;
+}
+
+/**
+ * @brief Find the index value of the given key string array
+ * @return Corresponding index. Returns -1 if not found.
+ * @param strv Null terminated array of gchar *
+ * @param key The key string value
+ */
+int find_key_strv(const gchar **strv, const gchar *key) {
+ int cursor = 0;
+
+ g_assert(strv != NULL);
+ while (strv[cursor]) {
+ if (!g_ascii_strcasecmp(strv[cursor], key))
+ return cursor;
+ cursor++;
+ }
+
+ return -1; /* Not Found */
+}
--- /dev/null
+/**
+ * @file unittest_common.cpp
+ * @date 31 May 2018
+ * @author MyungJoo Ham <myungjoo.ham@samsung.com>
+ *
+ * @brief Unit test module for NNStreamer common library
+ *
+ * Copyright 2018 Samsung Electronics
+ *
+ */
+
+#include <gtest/gtest.h>
+#include <unistd.h>
+#include <tensor_common.h>
+
+TEST(common_get_tensor_type, int32_1) {
+ EXPECT_EQ(get_tensor_type("int32"), _NNS_INT32);
+}
+TEST(common_get_tensor_type, int32_2) {
+ EXPECT_EQ(get_tensor_type("INT32"), _NNS_INT32);
+}
+TEST(common_get_tensor_type, int32_3) {
+ EXPECT_EQ(get_tensor_type("iNt32"), _NNS_INT32);
+}
+TEST(common_get_tensor_type, int32_4) {
+ EXPECT_EQ(get_tensor_type("InT32"), _NNS_INT32);
+}
+TEST(common_get_tensor_type, int32_5) {
+ EXPECT_EQ(get_tensor_type("InT322"), _NNS_END);
+}
+TEST(common_get_tensor_type, int32_6) {
+ EXPECT_EQ(get_tensor_type("int3"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, int16_1) {
+ EXPECT_EQ(get_tensor_type("int16"), _NNS_INT16);
+}
+TEST(common_get_tensor_type, int16_2) {
+ EXPECT_EQ(get_tensor_type("INT16"), _NNS_INT16);
+}
+TEST(common_get_tensor_type, int16_3) {
+ EXPECT_EQ(get_tensor_type("iNt16"), _NNS_INT16);
+}
+TEST(common_get_tensor_type, int16_4) {
+ EXPECT_EQ(get_tensor_type("InT16"), _NNS_INT16);
+}
+TEST(common_get_tensor_type, int16_5) {
+ EXPECT_EQ(get_tensor_type("InT162"), _NNS_END);
+}
+TEST(common_get_tensor_type, int16_6) {
+ EXPECT_EQ(get_tensor_type("int1"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, int8_1) {
+ EXPECT_EQ(get_tensor_type("int8"), _NNS_INT8);
+}
+TEST(common_get_tensor_type, int8_2) {
+ EXPECT_EQ(get_tensor_type("INT8"), _NNS_INT8);
+}
+TEST(common_get_tensor_type, int8_3) {
+ EXPECT_EQ(get_tensor_type("iNt8"), _NNS_INT8);
+}
+TEST(common_get_tensor_type, int8_4) {
+ EXPECT_EQ(get_tensor_type("InT8"), _NNS_INT8);
+}
+TEST(common_get_tensor_type, int8_5) {
+ EXPECT_EQ(get_tensor_type("InT82"), _NNS_END);
+}
+TEST(common_get_tensor_type, int8_6) {
+ EXPECT_EQ(get_tensor_type("int3"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, uint32_1) {
+ EXPECT_EQ(get_tensor_type("uint32"), _NNS_UINT32);
+}
+TEST(common_get_tensor_type, uint32_2) {
+ EXPECT_EQ(get_tensor_type("UINT32"), _NNS_UINT32);
+}
+TEST(common_get_tensor_type, uint32_3) {
+ EXPECT_EQ(get_tensor_type("uiNt32"), _NNS_UINT32);
+}
+TEST(common_get_tensor_type, uint32_4) {
+ EXPECT_EQ(get_tensor_type("UInT32"), _NNS_UINT32);
+}
+TEST(common_get_tensor_type, uint32_5) {
+ EXPECT_EQ(get_tensor_type("UInT322"), _NNS_END);
+}
+TEST(common_get_tensor_type, uint32_6) {
+ EXPECT_EQ(get_tensor_type("uint3"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, uint16_1) {
+ EXPECT_EQ(get_tensor_type("uint16"), _NNS_UINT16);
+}
+TEST(common_get_tensor_type, uint16_2) {
+ EXPECT_EQ(get_tensor_type("UINT16"), _NNS_UINT16);
+}
+TEST(common_get_tensor_type, uint16_3) {
+ EXPECT_EQ(get_tensor_type("uiNt16"), _NNS_UINT16);
+}
+TEST(common_get_tensor_type, uint16_4) {
+ EXPECT_EQ(get_tensor_type("UInT16"), _NNS_UINT16);
+}
+TEST(common_get_tensor_type, uint16_5) {
+ EXPECT_EQ(get_tensor_type("UInT162"), _NNS_END);
+}
+TEST(common_get_tensor_type, uint16_6) {
+ EXPECT_EQ(get_tensor_type("uint1"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, uint8_1) {
+ EXPECT_EQ(get_tensor_type("uint8"), _NNS_UINT8);
+}
+TEST(common_get_tensor_type, uint8_2) {
+ EXPECT_EQ(get_tensor_type("UINT8"), _NNS_UINT8);
+}
+TEST(common_get_tensor_type, uint8_3) {
+ EXPECT_EQ(get_tensor_type("uiNt8"), _NNS_UINT8);
+}
+TEST(common_get_tensor_type, uint8_4) {
+ EXPECT_EQ(get_tensor_type("UInT8"), _NNS_UINT8);
+}
+TEST(common_get_tensor_type, uint8_5) {
+ EXPECT_EQ(get_tensor_type("UInT82"), _NNS_END);
+}
+TEST(common_get_tensor_type, uint8_6) {
+ EXPECT_EQ(get_tensor_type("uint3"), _NNS_END);
+}
+
+
+TEST(common_get_tensor_type, float32_1) {
+ EXPECT_EQ(get_tensor_type("float32"), _NNS_FLOAT32);
+}
+TEST(common_get_tensor_type, float32_2) {
+ EXPECT_EQ(get_tensor_type("FLOAT32"), _NNS_FLOAT32);
+}
+TEST(common_get_tensor_type, float32_3) {
+ EXPECT_EQ(get_tensor_type("float32"), _NNS_FLOAT32);
+}
+TEST(common_get_tensor_type, float32_4) {
+ EXPECT_EQ(get_tensor_type("FloaT32"), _NNS_FLOAT32);
+}
+TEST(common_get_tensor_type, float32_5) {
+ EXPECT_EQ(get_tensor_type("FloaT322"), _NNS_END);
+}
+TEST(common_get_tensor_type, float32_6) {
+ EXPECT_EQ(get_tensor_type("float3"), _NNS_END);
+}
+
+
+
+TEST(common_get_tensor_type, float64_1) {
+ EXPECT_EQ(get_tensor_type("float64"), _NNS_FLOAT64);
+}
+TEST(common_get_tensor_type, float64_2) {
+ EXPECT_EQ(get_tensor_type("FLOAT64"), _NNS_FLOAT64);
+}
+TEST(common_get_tensor_type, float64_3) {
+ EXPECT_EQ(get_tensor_type("float64"), _NNS_FLOAT64);
+}
+TEST(common_get_tensor_type, float64_4) {
+ EXPECT_EQ(get_tensor_type("FloaT64"), _NNS_FLOAT64);
+}
+TEST(common_get_tensor_type, float64_5) {
+ EXPECT_EQ(get_tensor_type("FloaT642"), _NNS_END);
+}
+TEST(common_get_tensor_type, float64_6) {
+ EXPECT_EQ(get_tensor_type("float6"), _NNS_END);
+}
+
+
+static const gchar* teststrv[] = {
+ "abcde",
+ "ABCDEF",
+ "1234",
+ "abcabc",
+ "tester",
+ NULL
+};
+TEST(common_find_key_strv, case1) {
+ EXPECT_EQ(find_key_strv(teststrv, "abcde"), 0);
+}
+TEST(common_find_key_strv, case2) {
+ EXPECT_EQ(find_key_strv(teststrv, "ABCDE"), 0);
+}
+TEST(common_find_key_strv, case3) {
+ EXPECT_EQ(find_key_strv(teststrv, "1234"), 2);
+}
+TEST(common_find_key_strv, case4) {
+ EXPECT_EQ(find_key_strv(teststrv, "tester"), 4);
+}
+TEST(common_find_key_strv, case5) {
+ EXPECT_EQ(find_key_strv(teststrv, "abcabcd"), -1);
+}
+
+
+int main(int argc, char **argv) {
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}