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