ut_heap.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 /* Max chunk size for 32-bit desktop target */
00026 #define HEAP_MAX_CHUNK_SIZE 2044
00027 
00028 /* Min chunk size for 32-bit desktop target */
00029 #define HEAP_CHUNK_MIN_SIZE 12
00030 
00036 void
00037 ut_heap_init_000(CuTest *tc)
00038 {
00039     PmReturn_t retval;
00040 
00041     retval = heap_init();
00042 
00043     CuAssertTrue(tc, retval == PM_RET_OK);
00044 
00045     CuAssertTrue(tc, heap_getAvail() > 0);
00046 }
00047 
00048 
00055 void
00056 ut_heap_getChunk_000(CuTest *tc)
00057 {
00058     uint8_t *pchunk;
00059     PmReturn_t retval;
00060     pPmObj_t pobj;
00061 
00062     retval = heap_init();
00063     retval = heap_getChunk(8, &pchunk);
00064     pobj = (pPmObj_t)pchunk;
00065 
00066     CuAssertTrue(tc, retval == PM_RET_OK);
00067     CuAssertPtrNotNull(tc, pchunk);
00068     CuAssertTrue(tc, OBJ_GET_SIZE(pobj) == HEAP_CHUNK_MIN_SIZE);
00069 }
00070 
00071 
00076 void
00077 ut_heap_getChunk_001(CuTest *tc)
00078 {
00079     uint8_t *pchunk;
00080     PmReturn_t retval;
00081     pPmObj_t pobj;
00082 
00083     retval = heap_init();
00084     retval = heap_getChunk(HEAP_MAX_CHUNK_SIZE, &pchunk);
00085     pobj = (pPmObj_t)pchunk;
00086 
00087     CuAssertTrue(tc, retval == PM_RET_OK);
00088     CuAssertPtrNotNull(tc, pchunk);
00089     CuAssertTrue(tc, OBJ_GET_SIZE(pobj) >= HEAP_MAX_CHUNK_SIZE);
00090 }
00091 
00092 
00098 void
00099 ut_heap_getAvail_000(CuTest *tc)
00100 {
00101     uint16_t avail1;
00102     uint16_t avail2;
00103     uint16_t actualsize;
00104     uint8_t *pchunk;
00105     PmReturn_t retval;
00106 
00107     retval = heap_init();
00108     avail1 = heap_getAvail();
00109 
00110     retval = heap_getChunk(16, &pchunk);
00111     actualsize = OBJ_GET_SIZE(pchunk);
00112     
00113     avail2 = heap_getAvail();
00114     CuAssertTrue(tc, (avail1 - avail2) == actualsize);
00115 }
00116 
00117 
00123 void
00124 ut_heap_freeChunk_000(CuTest *tc)
00125 {
00126     uint16_t avail1;
00127     uint16_t avail2;
00128     uint16_t actualsize;
00129     uint8_t *pchunk;
00130     PmReturn_t retval;
00131 
00132     retval = heap_init();
00133     retval = heap_getChunk(16, &pchunk);
00134     actualsize = OBJ_GET_SIZE(pchunk);
00135     avail1 = heap_getAvail();
00136     retval = heap_freeChunk((pPmObj_t)pchunk);
00137     CuAssertTrue(tc, retval == PM_RET_OK);
00138 
00139     avail2 = heap_getAvail();
00140     CuAssertTrue(tc, (avail2 - avail1) == actualsize);
00141 }
00142 
00143 
00148 void
00149 ut_heap_freeChunk_001(CuTest *tc)
00150 {
00151     uint16_t avail1;
00152     uint16_t avail2;
00153     uint8_t *pchunk1;
00154     uint8_t *pchunk2;
00155     uint8_t *pchunk3;
00156     PmReturn_t retval;
00157 
00158     retval = heap_init();
00159     avail1 = heap_getAvail();
00160     retval = heap_getChunk(19, &pchunk1);
00161     retval = heap_getChunk(33, &pchunk2);
00162     retval = heap_getChunk(88, &pchunk3);
00163     avail2 = heap_getAvail();
00164     CuAssertTrue(tc, avail1 - avail2 >= (19+33+88));
00165 
00166     retval = heap_freeChunk((pPmObj_t)pchunk1);
00167     retval = heap_freeChunk((pPmObj_t)pchunk2);
00168     retval = heap_freeChunk((pPmObj_t)pchunk3);
00169     CuAssertTrue(tc, retval == PM_RET_OK);
00170 
00171     avail2 = heap_getAvail();
00172     CuAssertTrue(tc, avail2 == avail1);
00173 }
00174 
00175 
00177 CuSuite *getSuite_testHeap(void)
00178 {
00179     CuSuite* suite = CuSuiteNew();
00180 
00181     SUITE_ADD_TEST(suite, ut_heap_init_000);
00182     SUITE_ADD_TEST(suite, ut_heap_getChunk_000);
00183     SUITE_ADD_TEST(suite, ut_heap_getChunk_001);
00184     SUITE_ADD_TEST(suite, ut_heap_getAvail_000);
00185     SUITE_ADD_TEST(suite, ut_heap_freeChunk_000);
00186     SUITE_ADD_TEST(suite, ut_heap_freeChunk_001);
00187 
00188     return suite;
00189 }

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