ut_dict.c

00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall.
00003 #
00004 # This file is part of the Python-on-a-Chip program.
00005 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00006 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00007 #
00008 # Python-on-a-Chip is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00012 # is seen in the file COPYING up one directory from this.
00013 */
00014 
00015 
00022 #include "CuTest.h"
00023 #include "pm.h"
00024 
00025 
00034 void
00035 ut_dict_new_000(CuTest* tc)
00036 {
00037     pPmObj_t pobj = C_NULL;
00038     PmReturn_t retval;
00039 
00040     retval = pm_init(MEMSPACE_RAM, C_NULL);
00041     retval = dict_new(&pobj);
00042 
00043     CuAssertTrue(tc, retval == PM_RET_OK);
00044     CuAssertPtrNotNull(tc, pobj);
00045     CuAssertTrue(tc, OBJ_GET_TYPE(pobj) == OBJ_TYPE_DIC);
00046     CuAssertTrue(tc, ((pPmDict_t)pobj)->length == 0);
00047 }
00048 
00049 
00056 void
00057 ut_dict_setItem_000(CuTest* tc)
00058 {
00059     pPmObj_t pobj = C_NULL;
00060     pPmObj_t pobj_orig;
00061     PmReturn_t retval;
00062 
00063     retval = pm_init(MEMSPACE_RAM, C_NULL);
00064     retval = dict_new(&pobj);
00065     pobj_orig = pobj;
00066 
00067     retval = dict_setItem(pobj, PM_ZERO, PM_ONE);
00068     CuAssertTrue(tc, retval == PM_RET_OK);
00069     CuAssertPtrNotNull(tc, pobj);
00070     CuAssertPtrEquals(tc, pobj_orig, pobj);
00071 }
00072 
00079 void
00080 ut_dict_setItem_001(CuTest* tc)
00081 {
00082     pPmObj_t pobj = C_NULL;
00083     pPmObj_t pval;
00084     PmReturn_t retval;
00085 
00086     retval = pm_init(MEMSPACE_RAM, C_NULL);
00087     retval = dict_new(&pobj);
00088 
00089     retval = dict_setItem(pobj, PM_ZERO, PM_ONE);
00090     CuAssertTrue(tc, ((pPmDict_t)pobj)->length == 1);
00091     retval = dict_getItem(pobj, PM_ZERO, &pval);
00092     CuAssertTrue(tc, retval == PM_RET_OK);
00093     CuAssertPtrEquals(tc, PM_ONE, pval);
00094     retval = dict_setItem(pobj, PM_ZERO, PM_NEGONE);
00095     retval = dict_getItem(pobj, PM_ZERO, &pval);
00096     CuAssertTrue(tc, retval == PM_RET_OK);
00097     CuAssertPtrEquals(tc, PM_NEGONE, pval);
00098 }
00099 
00100 
00109 void
00110 ut_dict_clear_000(CuTest *tc)
00111 {
00112     pPmObj_t pobj = C_NULL;
00113     PmReturn_t retval;
00114 
00115     retval = pm_init(MEMSPACE_RAM, C_NULL);
00116     retval = dict_new(&pobj);
00117 
00118     retval = dict_clear(PM_ONE);
00119     CuAssertTrue(tc, retval == PM_RET_EX_TYPE);
00120     retval = dict_clear(pobj);
00121     CuAssertTrue(tc, retval == PM_RET_OK);
00122     CuAssertPtrNotNull(tc, pobj);
00123     CuAssertTrue(tc, ((pPmDict_t)pobj)->length == 0);
00124     retval = dict_setItem(pobj, PM_ZERO, PM_ONE);
00125     retval = dict_clear(pobj);
00126     CuAssertTrue(tc, retval == PM_RET_OK);
00127     CuAssertTrue(tc, ((pPmDict_t)pobj)->length == 0);
00128 }
00129 
00130 
00139 void
00140 ut_dict_getItem_000(CuTest *tc)
00141 {
00142     pPmObj_t pobj = C_NULL;
00143     pPmObj_t pval;
00144     PmReturn_t retval;
00145 
00146     retval = pm_init(MEMSPACE_RAM, C_NULL);
00147     retval = dict_new(&pobj);
00148 
00149     retval = dict_getItem(PM_ONE, PM_ONE, &pval);
00150     CuAssertTrue(tc, retval == PM_RET_EX_TYPE);
00151     retval = dict_new(&pobj);
00152     retval = dict_getItem(pobj, pobj, &pval);
00153     CuAssertTrue(tc, retval == PM_RET_EX_KEY);
00154     retval = dict_new(&pobj);
00155     retval = dict_getItem(pobj, PM_ONE, &pval);
00156     CuAssertTrue(tc, retval == PM_RET_EX_KEY);
00157     retval = dict_new(&pobj);
00158     retval = dict_setItem(pobj, PM_ONE, PM_ZERO);
00159     retval = dict_getItem(pobj, PM_NEGONE, &pval);
00160     CuAssertTrue(tc, retval == PM_RET_EX_KEY);
00161     retval = dict_new(&pobj);
00162     retval = dict_setItem(pobj, PM_ONE, PM_ZERO);
00163     retval = dict_getItem(pobj, PM_ONE, &pval);
00164     CuAssertTrue(tc, retval == PM_RET_OK);
00165     CuAssertTrue(tc, pval == PM_ZERO);
00166 }
00167 
00168 
00169 /* BEGIN unit tests ported from Snarf */
00170 
00171 char *test_str1 = "zzang1";
00172 char *test_str2 = "zzang2";
00173 char *test_str3 = "zzang3";
00174 char *test_strnew = "zzangnew";
00175 
00176 char *p_test_str1;
00177 char *p_test_str2;
00178 char *p_test_str3;
00179 char *p_test_strnew;
00180 
00181 pPmObj_t p_dict;
00182 pPmObj_t p_dict2;
00183 pPmObj_t p_tempobj;
00184 pPmObj_t p_tempobj2;
00185 pPmObj_t p_list;
00186 
00187 pPmInt_t p_firstkey;
00188 pPmInt_t p_secondkey;
00189 pPmInt_t p_thirdkey;
00190 pPmInt_t p_newkey;
00191 
00192 pPmObj_t p_firstval;
00193 pPmObj_t p_secondval;
00194 pPmObj_t p_thirdval;
00195 pPmObj_t p_newval;
00196 
00197 char sol;
00198 
00205 void
00206 ut_dict_getItem_001(CuTest *tc)
00207 {
00208     p_test_str1 = test_str1;
00209     p_test_str2 = test_str2;
00210     p_test_str3 = test_str3;
00211     p_test_strnew = test_strnew;
00212     PmReturn_t retval;
00213     
00214     retval = pm_init(MEMSPACE_RAM, C_NULL);
00215     
00216     retval = string_new((uint8_t const **)&test_str1, &p_firstval);
00217     retval = string_new((uint8_t const **)&test_str2, &p_secondval);
00218     retval = string_new((uint8_t const **)&test_str3, &p_thirdval);
00219     retval = string_new((uint8_t const **)&test_strnew, &p_newval);
00220     
00221     retval = int_new(1, (pPmObj_t *)&p_firstkey);
00222     retval = int_new(2, (pPmObj_t *)&p_secondkey);
00223     retval = int_new(3, (pPmObj_t *)&p_thirdkey);
00224     retval = int_new(2, (pPmObj_t *)&p_newkey);
00225     
00226     dict_new(&p_dict);
00227     
00228     dict_setItem(p_dict, (pPmObj_t)p_firstkey, p_firstval);
00229     dict_setItem(p_dict, (pPmObj_t)p_secondkey, p_secondval);
00230     dict_setItem(p_dict, (pPmObj_t)p_thirdkey, p_thirdval);
00231     dict_setItem(p_dict, (pPmObj_t)p_newkey, p_newval);
00232     dict_setItem(p_dict, (pPmObj_t)p_secondkey, p_secondval);
00233     
00234     dict_getItem(p_dict, (pPmObj_t)p_firstkey, &p_tempobj);
00235     CuAssertTrue(tc, obj_compare(p_tempobj, p_firstval) == C_SAME);
00236     
00237     dict_getItem(p_dict, (pPmObj_t)p_secondkey, &p_tempobj);
00238     CuAssertTrue(tc, obj_compare(p_tempobj, p_secondval) == C_SAME);
00239     
00240     dict_getItem(p_dict, (pPmObj_t)p_thirdkey, &p_tempobj);
00241     CuAssertTrue(tc, obj_compare(p_tempobj, p_thirdval) == C_SAME);
00242 }
00243 
00244 
00245 #if 0
00246 
00250 void
00251 ut_dict_hasKey_000(CuTest *tc)
00252 {
00253   if (dict_hasKey(p_dict, (pPmObj_t)p_firstkey)==0) return 0;
00254   if (dict_hasKey(p_dict, (pPmObj_t)p_secondkey)==0) return 0;
00255   if (dict_hasKey(p_dict, (pPmObj_t)p_thirdkey)==0) return 0;
00256 }
00257 
00258 
00259 /* This tests if it returns the correct list of keys and vals */
00260 U8
00261 Dict_test2(void)
00262 {
00263   dict_keys(p_dict, &p_list);
00264 
00265   list_getItem(p_list, 0, &p_tempobj);
00266   if (object_isEqual(p_tempobj,(pPmObj_t) p_firstkey)==0) return 0 ;
00267 
00268   list_getItem(p_list, 1, &p_tempobj);
00269   if (object_isEqual(p_tempobj,(pPmObj_t) p_secondkey)==0) return 0 ;
00270 
00271   list_getItem(p_list, 2, &p_tempobj);
00272   if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdkey)==0) return 0 ;
00273 
00274   //destroy_chunk(p_list); //destroys elements in p_list. Destroys contents the keys! bad
00275 
00276   dict_vals(p_dict, &p_list);
00277   list_getItem(p_list, 0, &p_tempobj);
00278   if (object_isEqual(p_tempobj,(pPmObj_t) p_firstval)==0) return 0 ;
00279 
00280   list_getItem(p_list, 1, &p_tempobj);
00281   if (object_isEqual(p_tempobj,(pPmObj_t) p_secondval)==0) return 0 ;
00282 
00283   list_getItem(p_list, 2, &p_tempobj);
00284   if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdval)==0) return 0 ;
00285 
00286   //NO. Destroys contents. We want to reuse for other tests.
00287   //destroy_chunk(p_list);
00288   //destroy_chunk(p_tempobj);
00289 
00290   return 1;
00291 }
00292 
00293 /* This tests if it pops correctly. */
00294 U8
00295 Dict_test3(void)
00296 {
00297   dict_popItem(p_dict, &p_tempobj, &p_tempobj2);
00298 
00299   if (object_isEqual(p_tempobj,(pPmObj_t) p_firstkey)==0) return 0 ;
00300   if (object_isEqual(p_tempobj2,(pPmObj_t) p_firstval)==0) return 0 ;
00301 
00302   dict_getItemIndex(p_dict, 0, &p_tempobj, &p_tempobj2);
00303   if (object_isEqual(p_tempobj,(pPmObj_t) p_secondkey)==0) return 0 ;
00304   if (object_isEqual(p_tempobj2 ,(pPmObj_t) p_secondval)==0) return 0 ;
00305 
00306   dict_getItemIndex(p_dict, 1, &p_tempobj, &p_tempobj2);
00307   if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdkey)==0) return 0 ;
00308   if (object_isEqual(p_tempobj2,(pPmObj_t) p_thirdval)==0) return 0 ;
00309 
00310   //NO.
00311   //destroy_chunk(p_tempobj);
00312   //destroy_chunk(p_tempobj2);
00313 
00314   return 1;
00315 }
00316 
00317 /* This tests if it updates correctly.(combines 2 dicts into one)*/
00318 U8
00319 Dict_test4(void)
00320 {
00321   dict_new(&p_dict2);
00322   dict_setItem(p_dict2,(pPmObj_t) p_firstkey, p_firstval);
00323   dict_update(&p_list, p_dict2, p_dict);
00324 
00325   dict_getItemIndex(p_list, 0, &p_tempobj, &p_tempobj2);
00326   if (object_isEqual(p_tempobj,(pPmObj_t) p_firstkey)==0) return 0 ;
00327   if (object_isEqual(p_tempobj2,(pPmObj_t) p_firstval)==0) return 0 ;
00328 
00329   dict_getItemIndex(p_list, 1, &p_tempobj, &p_tempobj2);
00330   if (object_isEqual(p_tempobj,(pPmObj_t) p_secondkey)==0) return 0 ;
00331   if (object_isEqual(p_tempobj2 ,(pPmObj_t) p_secondval)==0) return 0 ;
00332 
00333   dict_getItemIndex(p_list, 2, &p_tempobj, &p_tempobj2);
00334   if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdkey)==0) return 0 ;
00335   if (object_isEqual(p_tempobj2 ,(pPmObj_t) p_thirdval)==0) return 0 ;
00336 
00337   //destroy_chunk(p_dict);
00338   //destroy_chunk(p_dict2);
00339   //destroy_chunk(p_list);
00340   //destroy_chunk(p_tempobj);
00341   //destroy_chunk(p_tempobj2);
00342   //Should destroy all items created above, 2 dicts, one list. All have overlapping contents
00343 
00344   return 1;
00345 }
00346 #endif
00347 /* END unit tests ported from Snarf */
00348 
00349 
00351 CuSuite *getSuite_testDict(void)
00352 {
00353     CuSuite* suite = CuSuiteNew();
00354 
00355     SUITE_ADD_TEST(suite, ut_dict_new_000);
00356     SUITE_ADD_TEST(suite, ut_dict_setItem_000);
00357     SUITE_ADD_TEST(suite, ut_dict_setItem_001);
00358     SUITE_ADD_TEST(suite, ut_dict_clear_000);
00359     SUITE_ADD_TEST(suite, ut_dict_getItem_000);
00360 
00361     SUITE_ADD_TEST(suite, ut_dict_getItem_001);
00362 
00363     return suite;
00364 }

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9