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-2023, Mavimax, Ltd. All Rights Reserved.
0009  * This software is released under one of the following licenses:
0010  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0011  * See LICENSE file for full text.
0012  * -----------------------------------------------------------------------------
0013  * AGPL license:
0014  *
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU Affero General Public License, version 3 as published
0017  * by the Free Software Foundation;
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 Affero General Public License, version 3
0022  * for more details.
0023  *
0024  * You should have received a copy of the GNU Affero General Public License along 
0025  * with this program; if not, write to the Free Software Foundation, Inc.,
0026  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0027  *
0028  * -----------------------------------------------------------------------------
0029  * A commercial use license is available from Mavimax, Ltd
0030  * contact@mavimax.com
0031  * -----------------------------------------------------------------------------
0032  */
0033 
0034 #include <stdio.h>
0035 #include <stdlib.h>
0036 #include <cgreen/cgreen.h>
0037 #include <ubf.h>
0038 #include <ndrstandard.h>
0039 #include <string.h>
0040 #include "test.fd.h"
0041 #include "ubfunit1.h"
0042 
0043 /**
0044  * Test Balloc
0045  * @return
0046  */
0047 Ensure(test_Balloc_Bfree)
0048 {
0049     UBFH *p_ub = NULL;
0050     int i;
0051     long j;
0052     char block[1230];
0053     /* will check with valgrind - do we have memory leaks or not */
0054     
0055     for (i=0; i<10; i++)
0056     {
0057         p_ub=Balloc(20, sizeof(block)*20);
0058         assert_not_equal(p_ub, NULL);
0059         
0060         for (j=0; j<20; j++)
0061         {
0062             assert_equal(Badd(p_ub, T_CARRAY_FLD, block, sizeof(block)), EXSUCCEED);
0063         }
0064         
0065         /* this one shall fail as no space ...*/
0066         /* lets leave some few for alignment 
0067          * if payload is big enough, no add data needed
0068         for (j=0; j<10; j++)
0069         {
0070             Badd(p_ub, T_CARRAY_FLD, block, sizeof(block));
0071         }
0072         */
0073         
0074         assert_equal(Badd(p_ub, T_CARRAY_FLD, block, sizeof(block)), EXFAIL);
0075         assert_equal(Berror, BNOSPACE);
0076         
0077         /* Feature #393
0078          * ensure that we got a correct size estimate with working buffer 
0079          */
0080         assert_equal(Bneeded(20, sizeof(block)*20), Bsizeof(p_ub));
0081         
0082         assert_equal(Bfree(p_ub), EXSUCCEED);
0083     }
0084 }
0085 
0086 /**
0087  * Basic test for reallocation
0088  */
0089 Ensure(test_Brealloc)
0090 {
0091     UBFH *p_ub = NULL;
0092     int i;
0093     long j;
0094     int loop;
0095     char block[1230];
0096     /* will check with valgrind - do we have memory leaks or not */
0097     
0098     for (i=0; i<10; i++)
0099     {
0100         p_ub=Balloc(20, sizeof(block)*20);
0101         assert_not_equal(p_ub, NULL);
0102         
0103         for (j=0; j<20; j++)
0104         {
0105             assert_equal(Badd(p_ub, T_CARRAY_FLD, block, sizeof(block)), EXSUCCEED);
0106         }
0107         
0108         /* this one shall fail as no space ...*/
0109         /* lets leave some few for alignment 
0110         for (j=0; j<10; j++)
0111         {
0112             Badd(p_ub, T_CARRAY_FLD, block, sizeof(block));
0113         }
0114         */
0115         
0116         assert_equal(Badd(p_ub, T_CARRAY_FLD, block, sizeof(block)), EXFAIL);
0117         assert_equal(Berror, BNOSPACE);
0118         
0119         /* now realloc... to 40 and 40 shall fill in */
0120         p_ub=Balloc(40, sizeof(block)*40);
0121         assert_not_equal(p_ub, NULL);
0122         
0123         loop = 40 - Bnum(p_ub);
0124         for (j=0; j<loop; j++)
0125         {
0126             Badd(p_ub, T_CARRAY_FLD, block, sizeof(block));
0127         }
0128         
0129         /* this one shall fail as no space ...*/
0130         /* lets leave some few for alignment 
0131         for (j=0; j<10; j++)
0132         {
0133             Badd(p_ub, T_CARRAY_FLD, block, sizeof(block));
0134         }
0135         */
0136         
0137         assert_equal(Badd(p_ub, T_CARRAY_FLD, block, sizeof(block)), EXFAIL);
0138         assert_equal(Berror, BNOSPACE);
0139         
0140         assert_equal(Bfree(p_ub), EXSUCCEED);
0141 
0142     }
0143 
0144 }
0145 
0146 TestSuite *ubf_mem_tests(void)
0147 {
0148     TestSuite *suite = create_test_suite();
0149 
0150     add_test(suite, test_Balloc_Bfree);
0151     add_test(suite, test_Brealloc);
0152 
0153     return suite;
0154 }
0155 
0156 /* vim: set ts=4 sw=4 et smartindent: */