END_TEST START_TEST (test_string) { char *test = "test"; OmlValue v, v2; OmlValueU vu; size_t bcount = xmembytes(); size_t alloc_diff; oml_value_init(&v); oml_value_init(&v2); omlc_zero(vu); /* Prepare the OmlValueU to be duplicated in the OmlValue */ omlc_set_const_string(vu, test); oml_value_set(&v, &vu, OML_STRING_VALUE); alloc_diff = xmembytes() - bcount - sizeof(size_t); bcount = xmembytes(); fail_if(alloc_diff < strlen(test) + 1, "OmlValue string allocated memory not big enough (%d instead of at least %d)", alloc_diff, strlen(test) + 1); oml_value_duplicate(&v2, &v); fail_if(omlc_get_string_ptr(*oml_value_get_value(&v2)) == omlc_get_string_ptr(*oml_value_get_value(&v)), "Copied OmlValue string pointer not allocated properly"); fail_if(strcmp(omlc_get_string_ptr(*oml_value_get_value(&v2)), omlc_get_string_ptr(*oml_value_get_value(&v))), "Copied OmlValue string mismatch ('%s' instead of '%s')", omlc_get_string_ptr(*oml_value_get_value(&v2)), omlc_get_string_ptr(*oml_value_get_value(&v))); fail_unless(omlc_get_string_length(*oml_value_get_value(&v2)) == strlen(omlc_get_string_ptr(*oml_value_get_value(&v2))), "Copied OmlValue string length not set properly (%d instead of %d)", omlc_get_string_length(*oml_value_get_value(&v2)), strlen(omlc_get_string_ptr(*oml_value_get_value(&v2))) + 1); alloc_diff = xmembytes() - bcount - sizeof(size_t); /* oml_malloc() always allocates at least sizeof(size_t) more for bookkeeping */ bcount = xmembytes(); fail_if(alloc_diff < strlen(omlc_get_string_ptr(*oml_value_get_value(&v))) + 1, "Copied OmlValue string allocated memory not big enough (%d instead of at least %d)", alloc_diff, strlen(omlc_get_string_ptr(*oml_value_get_value(&v))) + 1); fail_unless(omlc_get_string_size(*oml_value_get_value(&v2)) == oml_malloc_usable_size(omlc_get_string_ptr(*oml_value_get_value(&v2))), "Copied OmlValue string allocated size not set properly (%d instead of %d)", omlc_get_string_size(*oml_value_get_value(&v2)), oml_malloc_usable_size(omlc_get_string_ptr(*oml_value_get_value(&v2)))); fail_unless(omlc_get_string_is_const(*oml_value_get_value(&v2)) == 0, "Copied OmlValue string should not be constant"); oml_value_set_type(&v, OML_UINT64_VALUE); fail_unless(xmembytes() < bcount, "OmlValue string was not freed after oml_value_set_type() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v2); fail_unless(xmembytes() < bcount, "OmlValue string was not freed after oml_value_reset() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v); omlc_reset_string(vu); }
END_TEST START_TEST (test_blob) { char *str = "this is a string subtly disguised as a blob"; OmlValue v, v2; OmlValueU vu; size_t len = strlen(str); void *test = (void*)str; size_t bcount; size_t alloc_diff; oml_value_init(&v); oml_value_init(&v2); omlc_zero(vu); /* Prepare the OmlValueU to be duplicated in the OmlValue */ omlc_set_blob(vu, test, len); bcount = xmembytes(); oml_value_set(&v, &vu, OML_BLOB_VALUE); alloc_diff = xmembytes() - bcount - sizeof(size_t); bcount = xmembytes(); fail_if(alloc_diff < len, "OmlValue blob allocated memory not big enough (%d instead of at least %d)", alloc_diff, len); oml_value_duplicate(&v2, &v); fail_if(omlc_get_blob_ptr(*oml_value_get_value(&v2)) == omlc_get_blob_ptr(*oml_value_get_value(&v)), "Copied OmlValue blob pointer not allocated properly"); fail_if(strncmp(omlc_get_blob_ptr(*oml_value_get_value(&v2)), omlc_get_blob_ptr(*oml_value_get_value(&v)), len), "Copied OmlValue blob mismatch"); fail_unless(omlc_get_blob_length(*oml_value_get_value(&v2)) == omlc_get_blob_length(*oml_value_get_value(&v)), "Copied OmlValue blob length not set properly (%d instead of %d)", omlc_get_blob_length(*oml_value_get_value(&v2)), omlc_get_blob_length(*oml_value_get_value(&v2))); alloc_diff = xmembytes() - bcount - sizeof(size_t); /* oml_malloc() always allocates at least sizeof(size_t) more for bookkeeping */ bcount = xmembytes(); fail_if(alloc_diff < len, "Copied OmlValue blob allocated memory not big enough (%d instead of at least %d)", alloc_diff, len); fail_unless(omlc_get_blob_size(*oml_value_get_value(&v2)) == oml_malloc_usable_size(omlc_get_blob_ptr(*oml_value_get_value(&v2))), "Copied OmlValue blob allocated size not set properly (%d instead of %d)", omlc_get_blob_size(*oml_value_get_value(&v2)), oml_malloc_usable_size(omlc_get_blob_ptr(*oml_value_get_value(&v2)))); oml_value_set_type(&v, OML_UINT64_VALUE); fail_unless(xmembytes() < bcount, "OmlValue blob was not freed after oml_value_set_type() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v2); fail_unless(xmembytes() < bcount, "OmlValue blob was not freed after oml_value_reset() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v); omlc_reset_blob(vu); }
static int sample( OmlFilter* f, OmlValue * value //! values of sample ) { InstanceData* self = (InstanceData*)f->instance_data; OmlValueU* v = oml_value_get_value(value);; OmlValueT type = oml_value_get_type(value);; if (type != self->result[0].type) { logwarn ("%s filter: Discarding sample type (%s) different from initial definition (%s)\n", FILTER_NAME, oml_type_to_s(type), oml_type_to_s(self->result[0].type)); return 0; } self->sample_count++; /* Overwrite previously stored value */ return oml_value_set(&self->result[0], v, type); }
/** Deep copy an OmlValue. * * \param dst OmlValue to copy to * \param src OmlValue to copy * \return 0 on success, -1 otherwise * \see oml_value_init, oml_value_set, oml_value_get_value, oml_value_get_type */ int oml_value_duplicate(OmlValue* dst, OmlValue* src) { return oml_value_set(dst, oml_value_get_value(src), oml_value_get_type(src)); }
/** DEPRECATED \see oml_value_set */ int oml_value_copy(OmlValueU *value, OmlValueT type, OmlValue *to) { logwarn("%s() is deprecated, please use oml_value_set(to, value, type) instead\n", __FUNCTION__); return oml_value_set(to, value, type); }