/* $Id: cpl_property-test.c,v 1.9 2007/07/16 09:09:03 llundin Exp $ * * This file is part of the ESO Common Pipeline Library * Copyright (C) 2001-2005 European Southern Observatory * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * $Author: llundin $ * $Date: 2007/07/16 09:09:03 $ * $Revision: 1.9 $ * $Name: $ */ #undef CX_DISABLE_ASSERT #undef CX_LOG_DOMAIN #include #include #include #include #include "cpl_init.h" #include "cpl_property.h" static void cpl_test_property_dump(cpl_property *property) { const cxchar *name = cpl_property_get_name(property); const cxchar *comment = cpl_property_get_comment(property); cxchar c; long size = cpl_property_get_size(property); cpl_type type = cpl_property_get_type(property); fprintf(stderr, "Property at address %p\n", property); fprintf(stderr, "\tname : %p '%s'\n", name, name); fprintf(stderr, "\tcomment: %p '%s'\n", comment, comment); fprintf(stderr, "\ttype : %#09x\n", type); fprintf(stderr, "\tsize : %ld\n", size); fprintf(stderr, "\tvalue : "); switch (type) { case CPL_TYPE_CHAR: c = cpl_property_get_char(property); if (!c) fprintf(stderr, "''"); else fprintf(stderr, "'%c'", c); break; case CPL_TYPE_BOOL: fprintf(stderr, "%d", cpl_property_get_bool(property)); break; case CPL_TYPE_INT: fprintf(stderr, "%d", cpl_property_get_int(property)); break; case CPL_TYPE_LONG: fprintf(stderr, "%ld", cpl_property_get_long(property)); break; case CPL_TYPE_FLOAT: fprintf(stderr, "%g", cpl_property_get_float(property)); break; case CPL_TYPE_DOUBLE: fprintf(stderr, "%g", cpl_property_get_double(property)); break; case CPL_TYPE_STRING: fprintf(stderr, "'%s'", cpl_property_get_string(property)); break; default: fprintf(stderr, "unknown."); break; } fprintf(stderr, "\n"); return; } int main(void) { cxsize i, j; const cxchar *names[] = { "char", "bool", "int", "long", "float", "double", "string" }; const cxchar *strings[] = { "Mary", "Mary had a little lamb", "Mary had a" }; cxfloat fval[] = {1.23456789, 0}; cxdouble dval[] = {1.23456789, 0}; cpl_type types[] = { CPL_TYPE_CHAR, CPL_TYPE_BOOL, CPL_TYPE_INT, CPL_TYPE_LONG, CPL_TYPE_FLOAT, CPL_TYPE_DOUBLE, CPL_TYPE_STRING }; cpl_property *property; cpl_property *plist[7]; cpl_init(CPL_INIT_DEFAULT); /* * Test 1: Create a properties of different types, check their validity * and destroy them again. */ for (i = 0; i < 7; i++) { plist[i] = cpl_property_new(names[i], types[i]); cx_assert(plist[i] != NULL); cx_assert(strcmp(cpl_property_get_name(plist[i]), names[i]) == 0); cx_assert(cpl_property_get_comment(plist[i]) == NULL); cx_assert(cpl_property_get_type(plist[i]) == types[i]); cx_assert(cpl_property_get_size(plist[i])) ; cpl_property_delete(plist[i]); } /* * Test 2: Create properties of different size and types, verify the * properties and destroy them again. Note that only types * having the array flag set will have sizes different from * one. */ for (i = 0; i < 7; i++) for (j = 1; j <= 100; j += 10) { property = cpl_property_new_array(names[i], types[i], j); cx_assert(property != NULL); cx_assert(strcmp(cpl_property_get_name(property), names[i]) == 0); cx_assert(cpl_property_get_comment(property) == NULL); cx_assert(cpl_property_get_type(property) == types[i]); cx_assert(cpl_property_get_size(property)); cpl_property_delete(property); } /* * Test 3: Create a properties of different types, set their comments, * modify their names and verify the new settings. */ for (i = 0; i < 7; i++) { plist[i] = cpl_property_new(names[i], types[i]); cpl_property_set_comment(plist[i], strings[0]); cx_assert(!strcmp(cpl_property_get_comment(plist[i]), strings[0])); cpl_property_set_comment(plist[i], strings[1]); cx_assert(!strcmp(cpl_property_get_comment(plist[i]), strings[1])); cpl_property_set_comment(plist[i], strings[2]); cx_assert(!strcmp(cpl_property_get_comment(plist[i]), strings[2])); cpl_property_set_comment(plist[i], NULL); cx_assert(cpl_property_get_comment(plist[i]) == NULL); } for (i = 0; i < 7; i++) { cpl_property_set_name(plist[i], names[6 - i]); cx_assert(!strcmp(cpl_property_get_name(plist[i]), names[6 - i])); cpl_property_delete(plist[i]); } /* * Test 4: Create properties for the supported types, set their * values and verify them. */ for (i = 0; i < 7; i++) plist[i] = cpl_property_new(names[i], types[i]); cpl_property_set_char(plist[0], 'a'); cx_assert(cpl_property_get_char(plist[0]) == 'a'); cpl_property_set_bool(plist[1], 1); cx_assert(cpl_property_get_bool(plist[1]) == 1); cpl_property_set_int(plist[2], 100); cx_assert(cpl_property_get_int(plist[2]) == 100); cpl_property_set_long(plist[3], 10L); cx_assert(cpl_property_get_long(plist[3]) == 10L); cpl_property_set_float(plist[4], fval[0]); fval[1] = cpl_property_get_float(plist[4]); cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float))); cpl_property_set_double(plist[5], dval[0]); dval[1] = cpl_property_get_double(plist[5]); cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double))); /* * Try strings with different length to verify that the value is * properly resized. Note that the size of a string property includes * the byte for the trailing zero. */ cpl_property_set_string(plist[6], strings[0]); cx_assert(cpl_property_get_size(plist[6]) == ((long)strlen(strings[0]) + 1)); cx_assert(!strcmp(cpl_property_get_string(plist[6]), strings[0])); cpl_property_set_string(plist[6], strings[1]); cx_assert(cpl_property_get_size(plist[6]) == ((long)strlen(strings[1]) + 1)); cx_assert(!strcmp(cpl_property_get_string(plist[6]), strings[1])); cpl_property_set_string(plist[6], strings[2]); cx_assert(cpl_property_get_size(plist[6]) == ((long)strlen(strings[2]) + 1)); cx_assert(!strcmp(cpl_property_get_string(plist[6]), strings[2])); /* * Test 5: Use the properties from the previous test, copy them and * and verify that original and copy are identical but don't * share any resources. */ for (i = 0; i < 7; i++) { cpl_property_set_comment(plist[i], strings[1]); property = cpl_property_duplicate(plist[i]); cx_assert(property != plist[i]); cx_assert(cpl_property_get_name(property) != cpl_property_get_name(plist[i])); cx_assert(cpl_property_get_comment(property) != cpl_property_get_comment(plist[i])); cx_assert(cpl_property_get_size(property) == cpl_property_get_size(plist[i])); cx_assert(cpl_property_get_type(property) == cpl_property_get_type(plist[i])); switch (cpl_property_get_type(property)) { case CPL_TYPE_CHAR: cx_assert(cpl_property_get_char(property) == cpl_property_get_char(plist[i])); break; case CPL_TYPE_BOOL: cx_assert(cpl_property_get_bool(property) == cpl_property_get_bool(plist[i])); break; case CPL_TYPE_INT: cx_assert(cpl_property_get_int(property) == cpl_property_get_int(plist[i])); break; case CPL_TYPE_LONG: cx_assert(cpl_property_get_long(property) == cpl_property_get_long(plist[i])); break; case CPL_TYPE_FLOAT: fval[0] = cpl_property_get_float(property); fval[1] = cpl_property_get_float(plist[i]); cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float))); break; case CPL_TYPE_DOUBLE: dval[0] = cpl_property_get_double(property); dval[1] = cpl_property_get_double(plist[i]); cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double))); break; case CPL_TYPE_STRING: cx_assert(!strcmp(cpl_property_get_string(property), cpl_property_get_string(plist[i]))); break; default: /* This should never happen */ cpl_end(); return 1; break; } cpl_property_delete(plist[i]); cpl_property_delete(property); } /* * All tests succeeded */ cpl_end(); return 0; }