summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorvveremeva <vveremjova@luxoft.com>2016-01-15 14:00:25 +0200
committerVeremjova Veronica <vveremjova@luxoft.com>2016-01-18 16:39:38 +0200
commitb8890523441c410758f61675927e74cd3c2a4932 (patch)
treea901e3cf9dac53a7b9b0c6eebd68cd44048c495a /src/components
parent3e86daa9ac21ca6e6ebc7abffc694c611f87d33a (diff)
downloadsdl_core-b8890523441c410758f61675927e74cd3c2a4932.tar.gz
Move cast number in another type to utils
Added cast in convert utils Removed redundant commit Added test for compact json string in formatters
Diffstat (limited to 'src/components')
-rw-r--r--src/components/formatters/test/formatter_json_rpc_test.cc22
-rw-r--r--src/components/smart_objects/include/smart_objects/number_schema_item.h14
-rw-r--r--src/components/smart_objects/src/smart_object.cc45
-rw-r--r--src/components/utils/include/utils/convert_utils.h14
4 files changed, 37 insertions, 58 deletions
diff --git a/src/components/formatters/test/formatter_json_rpc_test.cc b/src/components/formatters/test/formatter_json_rpc_test.cc
index d065bd09a8..989b1e79fb 100644
--- a/src/components/formatters/test/formatter_json_rpc_test.cc
+++ b/src/components/formatters/test/formatter_json_rpc_test.cc
@@ -59,8 +59,9 @@ void CompactJson(std::string& str) {
reader.parse(str, root);
Json::FastWriter writer;
str = writer.write(root);
- std::string::iterator end_pos = std::remove(str.begin(), str.end(), '\n');
- str.erase(end_pos, str.end());
+ if (str[str.size() - 1] == '\n') {
+ str.erase(str.size()-1,1);
+ }
}
namespace {
@@ -68,6 +69,23 @@ const int64_t big_64int = 100000000000;
const std::string str_with_big_int64 = "100000000000";
} // namespace
+TEST(FormatterJsonRPCTest, CheckCompactJson){
+ std::string before_compact(
+ "{\n \"jsonrpc\" : \"2.0\",\n \"method\" : \"BasicCommunication.OnSystemRequest\","
+ "\n \"params\" : {\n \"fileName\" : \"file \n Name\",\n \"length\" : 100000000000,\n"
+ "\"offset\" : 100000000000,\n \"requestType\" : \"PROPRIETARY\"\n }\n}\n");
+ std::string after_compact = before_compact;
+ CompactJson(after_compact);
+
+ EXPECT_NE(before_compact, after_compact);
+
+ std::string expected(
+ "{\"jsonrpc\":\"2.0\",\"method\":\"BasicCommunication.OnSystemRequest\","
+ "\"params\":{\"fileName\":\"file \\n Name\",\"length\":100000000000,"
+ "\"offset\":100000000000,\"requestType\":\"PROPRIETARY\"}}");
+ EXPECT_EQ(expected, after_compact);
+}
+
TEST(FormatterJsonRPCTest, CorrectRPCv1Request_ToString_Success) {
// Create SmartObject
SmartObject obj;
diff --git a/src/components/smart_objects/include/smart_objects/number_schema_item.h b/src/components/smart_objects/include/smart_objects/number_schema_item.h
index ac84cab476..b97b20bd9f 100644
--- a/src/components/smart_objects/include/smart_objects/number_schema_item.h
+++ b/src/components/smart_objects/include/smart_objects/number_schema_item.h
@@ -38,6 +38,7 @@
#include "utils/shared_ptr.h"
#include "smart_objects/default_shema_item.h"
#include "smart_objects/schema_item_parameter.h"
+#include "utils/convert_utils.h"
namespace NsSmartDeviceLink {
namespace NsSmartObjects {
@@ -124,15 +125,6 @@ bool TNumberSchemaItem<NumberType>::isValidNumberType(SmartType type) {
}
}
-template <typename InputType, typename OutputType>
-OutputType IntStaticCast(const InputType& value) {
- DCHECK_OR_RETURN(value >= std::numeric_limits<OutputType>::min(),
- std::numeric_limits<OutputType>::min());
- DCHECK_OR_RETURN(value <= std::numeric_limits<OutputType>::max(),
- std::numeric_limits<OutputType>::max());
- return static_cast<OutputType>(value);
-}
-
template <typename NumberType>
Errors::eType TNumberSchemaItem<NumberType>::validate(
const SmartObject& Object) {
@@ -141,9 +133,9 @@ Errors::eType TNumberSchemaItem<NumberType>::validate(
}
NumberType value(0);
if (typeid(int32_t) == typeid(value)) {
- value = IntStaticCast<int64_t,int32_t>(Object.asInt());
+ value = utils::SafeStaticCast<int64_t,int32_t>(Object.asInt());
} else if (typeid(uint32_t) == typeid(value)) {
- value = IntStaticCast<uint64_t,uint32_t>(Object.asUInt());
+ value = utils::SafeStaticCast<uint64_t,uint32_t>(Object.asUInt());
} else if (typeid(double) == typeid(value)) {
value = Object.asDouble();
} else if (typeid(int64_t) == typeid(value)) {
diff --git a/src/components/smart_objects/src/smart_object.cc b/src/components/smart_objects/src/smart_object.cc
index 40e31cc771..5b6df0b5d9 100644
--- a/src/components/smart_objects/src/smart_object.cc
+++ b/src/components/smart_objects/src/smart_object.cc
@@ -170,9 +170,6 @@ bool SmartObject::operator==(const SmartObject& Other) const {
return false;
}
-// =============================================================
-// INTEGER TYPE SUPPORT
-// =============================================================
SmartObject::SmartObject(int32_t InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -231,9 +228,6 @@ int64_t SmartObject::convert_int() const {
return invalid_int64_value;
}
-// =============================================================
-// uint32_t TYPE SUPPORT
-// =============================================================
SmartObject::SmartObject(uint32_t InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -264,9 +258,6 @@ bool SmartObject::operator==(const uint32_t Value) const {
return comp == static_cast<int64_t>(Value);
}
-// =============================================================
-// int64_t TYPE SUPPORT
-// =============================================================
SmartObject::SmartObject(int64_t InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -289,9 +280,6 @@ bool SmartObject::operator==(const int64_t Value) const {
return comp == Value;
}
-// =============================================================
-// uint64_t TYPE SUPPORT
-// =============================================================
SmartObject& SmartObject::operator=(const uint64_t NewValue) {
if (m_type != SmartType_Invalid) {
set_value_integer(NewValue);
@@ -299,9 +287,6 @@ SmartObject& SmartObject::operator=(const uint64_t NewValue) {
return *this;
}
-// =============================================================
-// DOUBLE TYPE SUPPORT
-// =============================================================
SmartObject::SmartObject(double InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -349,10 +334,6 @@ double SmartObject::convert_double() const {
return invalid_double_value;
}
-// =============================================================
-// BOOL TYPE SUPPORT
-// =============================================================
-
SmartObject::SmartObject(bool InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -399,10 +380,6 @@ bool SmartObject::convert_bool() const {
return invalid_bool_value;
}
-// =============================================================
-// CHAR TYPE SUPPORT
-// =============================================================
-
SmartObject::SmartObject(char InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -448,10 +425,6 @@ char SmartObject::convert_char() const {
return invalid_char_value;
}
-// =============================================================
-// STD::STRING TYPE SUPPORT
-// =============================================================
-
SmartObject::SmartObject(const std::string& InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -509,10 +482,6 @@ std::string SmartObject::convert_string() const {
return NsSmartDeviceLink::NsSmartObjects::invalid_cstr_value;
}
-// =============================================================
-// CHAR* TYPE SUPPORT
-// =============================================================
-
SmartObject::SmartObject(const char* const InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -540,9 +509,6 @@ void SmartObject::set_value_cstr(const char* NewValue) {
set_value_string(NewValue ? std::string(NewValue) : std::string());
}
-// =============================================================
-// BINARY TYPE SUPPORT
-// =============================================================
SmartObject::SmartObject(const SmartBinary& InitialValue)
: m_type(SmartType_Null),
m_schema() {
@@ -593,10 +559,6 @@ SmartBinary SmartObject::convert_binary() const {
return invalid_binary_value;
}
-// =============================================================
-// ARRAY INTERFACE SUPPORT
-// =============================================================
-
SmartObject& SmartObject::operator[](const int32_t Index) {
return handle_array_access(Index);
}
@@ -628,10 +590,6 @@ inline SmartObject& SmartObject::handle_array_access(const int32_t Index) {
return invalid_object_value;
}
-// =============================================================
-// MAP INTERFACE SUPPORT
-// =============================================================
-
SmartObject& SmartObject::operator[](const std::string& Key) {
return handle_map_access(Key);
}
@@ -690,9 +648,6 @@ SmartObject& SmartObject::handle_map_access(const std::string& Key) {
return map[Key];
}
-// =============================================================
-// OTHER METHODS
-// =============================================================
void SmartObject::duplicate(const SmartObject& OtherObject) {
SmartData newData;
const SmartType newType = OtherObject.m_type;
diff --git a/src/components/utils/include/utils/convert_utils.h b/src/components/utils/include/utils/convert_utils.h
index 261dc7418e..b3e849d6bd 100644
--- a/src/components/utils/include/utils/convert_utils.h
+++ b/src/components/utils/include/utils/convert_utils.h
@@ -34,6 +34,7 @@
#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_
#include <stdint.h>
+#include <limits>
namespace utils {
@@ -59,6 +60,19 @@ unsigned long long int ConvertUInt64ToLongLongUInt(const uint64_t value);
*/
uint64_t ConvertLongLongUIntToUInt64(const unsigned long long int value);
+
+/**
+ * Convert one number value to another type value
+ */
+template <typename InputType, typename OutputType>
+OutputType SafeStaticCast(const InputType value) {
+ DCHECK_OR_RETURN(value >= std::numeric_limits<OutputType>::min(),
+ std::numeric_limits<OutputType>::min());
+ DCHECK_OR_RETURN(value <= std::numeric_limits<OutputType>::max(),
+ std::numeric_limits<OutputType>::max());
+ return static_cast<OutputType>(value);
+}
+
} // namespace utils
#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_