Back to home page

Enduro/X

 
 

    


0001 /* 
0002 **
0003 ** @file test_mem.c
0004 ** 
0005 ** -----------------------------------------------------------------------------
0006 ** Enduro/X Middleware Platform for Distributed Transaction Processing
0007 ** Copyright (C) 2015, Mavimax, Ltd. All Rights Reserved.
0008 ** This software is released under one of the following licenses:
0009 ** GPL or Mavimax's license for commercial use.
0010 ** -----------------------------------------------------------------------------
0011 ** GPL license:
0012 ** 
0013 ** This program is free software; you can redistribute it and/or modify it under
0014 ** the terms of the GNU General Public License as published by the Free Software
0015 ** Foundation; either version 2 of the License, or (at your option) any later
0016 ** version.
0017 **
0018 ** This program is distributed in the hope that it will be useful, but WITHOUT ANY
0019 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0020 ** PARTICULAR PURPOSE. See the GNU General Public License for more details.
0021 **
0022 ** You should have received a copy of the GNU General Public License along with
0023 ** this program; if not, write to the Free Software Foundation, Inc., 59 Temple
0024 ** Place, Suite 330, Boston, MA 02111-1307 USA
0025 **
0026 ** -----------------------------------------------------------------------------
0027 ** A commercial use license is available from Mavimax, Ltd
0028 ** contact@mavimax.com
0029 ** -----------------------------------------------------------------------------
0030 */
0031 
0032 #include <stdio.h>
0033 #include <stdlib.h>
0034 #include <cgreen/cgreen.h>
0035 #include <ubf.h>
0036 #include <ndrstandard.h>
0037 #include <string.h>
0038 #include "test.fd.h"
0039 #include "ubfunit1.h"
0040 
0041 /**
0042  * Test Balloc
0043  * @return
0044  */
0045 Ensure(test_Balloc_Bfree)
0046 {
0047     UBFH *p_ub = NULL;
0048     int i;
0049     /* will check with valgrind - do we have memory leaks or not */
0050     
0051     for (i=0; i<20; i++)
0052     {
0053         p_ub=Balloc(20, 30);
0054         assert_not_equal(p_ub, NULL);
0055         /* Put some data into memory so that we can test */
0056         set_up_dummy_data(p_ub);
0057         do_dummy_data_test(p_ub);
0058         assert_equal(Bfree(p_ub), EXSUCCEED);
0059 
0060     }
0061 }
0062 
0063 /**
0064  * Basic test for reallocation
0065  */
0066 Ensure(test_Brealloc)
0067 {
0068     UBFH *p_ub = NULL;
0069 
0070     p_ub=Balloc(1, 30);
0071     assert_not_equal(p_ub, NULL);
0072 
0073     assert_equal(Badd(p_ub, T_STRING_FLD, BIG_TEST_STRING, 0), EXFAIL);
0074     assert_equal(Berror, BNOSPACE);
0075 
0076     /* Now reallocate, space should be bigger! */
0077     p_ub=Brealloc(p_ub, 1, strlen(BIG_TEST_STRING)+1+2/* align */);
0078     assert_not_equal(p_ub, NULL);
0079     assert_equal(Badd(p_ub, T_STRING_FLD, BIG_TEST_STRING, 0), EXSUCCEED);
0080     
0081     /* should not allow to reallocate to 0! */
0082     assert_equal(Brealloc(p_ub, 1, 0), NULL);
0083     assert_equal(Berror, BEINVAL);
0084 
0085     /* should be bigger than existing. 4 is sizeof bfld, first in stuct */
0086     assert_equal(Brealloc(p_ub, 1, strlen(BIG_TEST_STRING)-4), NULL);
0087     assert_equal(Berror, BEINVAL);
0088 
0089     assert_equal(EXSUCCEED, Bfree(p_ub));
0090 
0091 }
0092 
0093 TestSuite *ubf_mem_tests(void)
0094 {
0095     TestSuite *suite = create_test_suite();
0096 
0097     add_test(suite, test_Balloc_Bfree);
0098     add_test(suite, test_Brealloc);
0099 
0100     return suite;
0101 }
0102