Back to home page

Enduro/X

 
 

    


0001 /* 
0002 **
0003 ** @file test_badd.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 
0043 #define DEFAULT_BUFFER  152+8
0044 UBFH *M_p_ub1 = NULL;
0045 
0046 /**
0047  * Basic preparation before the test
0048  */
0049 Ensure(basic_setup1)
0050 {
0051     /*printf("basic_setup\n"); */
0052     M_p_ub1 = malloc(DEFAULT_BUFFER);
0053     memset(M_p_ub1, 255, DEFAULT_BUFFER);
0054     if (EXFAIL==Binit(M_p_ub1, DEFAULT_BUFFER))
0055     {
0056         fprintf(stderr, "Binit failed!\n");
0057     }
0058 
0059     setenv("FLDTBLDIR", "./ubftab", 1);
0060     setenv("FIELDTBLS", "Exfields,test.fd", 1);
0061 }
0062 
0063 void basic_teardown1(void)
0064 {
0065     /*printf("basic_teardown\n");*/
0066     free (M_p_ub1);
0067 }
0068 
0069 /**
0070  * Basic test for Badd & Bget
0071  */
0072 Ensure(test_Badd_str)
0073 {
0074     char pan1[32] = "THIS IS 1";
0075     char pan2[32] = "THIS IS 2";
0076     char pan3[32] = "THIS IS 3";
0077     char pan4[32] = "THIS IS 4";
0078 
0079     double amttxn1=10021.123;
0080     double amttxn2=20021.123;
0081     double amttxn3=321.123;
0082     double amttxn4=11.123;
0083     double amttxn5=33.123;
0084     double tmp_amttxn=0;
0085 
0086     char tmp_buf[32+8]; /* 8 for alignment */
0087     int org_used = Bused(M_p_ub1);
0088 
0089     assert_equal(
0090             Badd(M_p_ub1, T_STRING_FLD, pan1, 0),
0091             EXSUCCEED);
0092 
0093     assert_equal(
0094             Badd(M_p_ub1, T_STRING_FLD, pan2, 0),
0095             EXSUCCEED);
0096 
0097     assert_equal(
0098             Badd(M_p_ub1, T_STRING_FLD, pan3, 0),
0099             EXSUCCEED);
0100 
0101     assert_equal(
0102             Badd(M_p_ub1, T_STRING_FLD, pan4, 0),
0103             EXSUCCEED);
0104 
0105     /* Get the data from buffer */
0106     assert_equal(
0107             Bget(M_p_ub1, T_STRING_FLD, 0, tmp_buf, 0),
0108             EXSUCCEED);
0109     assert_string_equal(pan1, tmp_buf);
0110 
0111     assert_equal(
0112             Bget(M_p_ub1, T_STRING_FLD, 1, tmp_buf, 0),
0113             EXSUCCEED);
0114     assert_string_equal(pan2, tmp_buf);
0115 
0116     assert_equal(
0117             Bget(M_p_ub1, T_STRING_FLD, 2, tmp_buf, 0),
0118             EXSUCCEED);
0119     assert_string_equal(pan3, tmp_buf);
0120 
0121     assert_equal(
0122             Bget(M_p_ub1, T_STRING_FLD, 3, tmp_buf, 0),
0123             EXSUCCEED);
0124     assert_string_equal(pan4, tmp_buf);
0125 
0126     /* Now add something other, some double value? */
0127     assert_equal(
0128             Badd(M_p_ub1, T_DOUBLE_4_FLD, (char *)&amttxn1, 0),
0129             EXSUCCEED);
0130     assert_equal(
0131             Badd(M_p_ub1, T_DOUBLE_4_FLD, (char *)&amttxn2, 0),
0132             EXSUCCEED);
0133     assert_equal(
0134             Badd(M_p_ub1, T_DOUBLE_4_FLD, (char *)&amttxn3, 0),
0135             EXSUCCEED);
0136     assert_equal(
0137             Badd(M_p_ub1, T_DOUBLE_4_FLD, (char *)&amttxn4, 0),
0138             EXSUCCEED);
0139     /* Do not have space availabel in buffer */
0140     assert_equal(
0141             Badd(M_p_ub1, T_DOUBLE_4_FLD, (char *)&amttxn5, 0),
0142             EXFAIL);
0143     /* Compare the values from buffer */
0144     assert_equal(
0145         Bget(M_p_ub1, T_DOUBLE_4_FLD, 0, (char *)&tmp_amttxn, 0),
0146         EXSUCCEED);
0147     assert_double_equal(tmp_amttxn, 10021.123);
0148 
0149     assert_equal(
0150             Bget(M_p_ub1, T_DOUBLE_4_FLD, 1, (char *)&tmp_amttxn, 0),
0151             EXSUCCEED);
0152     assert_double_equal(tmp_amttxn, 20021.123);
0153 
0154     assert_equal(
0155             Bget(M_p_ub1, T_DOUBLE_4_FLD, 2, (char *)&tmp_amttxn, 0),
0156             EXSUCCEED);
0157     assert_double_equal(tmp_amttxn, 321.123);
0158     assert_equal(
0159             Bget(M_p_ub1, T_DOUBLE_4_FLD, 3, (char *)&tmp_amttxn, 0),
0160             EXSUCCEED);
0161     assert_double_equal(tmp_amttxn, 11.123);
0162     /* Do not have space in buffer! */
0163     assert_equal(
0164             Bget(M_p_ub1, T_DOUBLE_4_FLD, 4, (char *)&tmp_amttxn, 0),
0165             EXFAIL);
0166 
0167     /* Summ the stuff up (+4 - EOS symbols in buffer)*/
0168     assert_equal(org_used+strlen(pan1)+strlen(pan2)+strlen(pan3)+strlen(pan4)+4+
0169                 sizeof(amttxn1)+sizeof(amttxn2)+sizeof(amttxn3)+sizeof(amttxn4)+
0170                 8*sizeof(BFLDID)+8/*align*/, Bused(M_p_ub1));
0171 }
0172 
0173 /**
0174  * Test of FAST add test
0175  * Single field in buffer
0176  */
0177 Ensure(test_Baddfast1)
0178 {
0179     Bfld_loc_info_t state;
0180     int i;
0181     char str[16];
0182     char buf1[56000];
0183     UBFH *p_ub1 = (UBFH *)buf1;
0184     
0185     char buf2[56000];
0186     UBFH *p_ub2 = (UBFH *)buf2;
0187 
0188     
0189     memset(buf1, 0, sizeof(buf1));
0190     memset(buf2, 0, sizeof(buf2));
0191     
0192     assert_equal(Binit(p_ub1, sizeof(buf1)), EXSUCCEED);
0193     assert_equal(Binit(p_ub2, sizeof(buf2)), EXSUCCEED);
0194     
0195     memset(&state, 0, sizeof(state));
0196     
0197     for (i=0; i<100; i++)
0198     {
0199     snprintf(str, sizeof(str), "%i", i);
0200     assert_equal(Baddfast(p_ub1, T_STRING_FLD, str, 0, &state), EXSUCCEED);
0201         assert_equal(Badd(p_ub2, T_STRING_FLD, str, 0), EXSUCCEED);
0202     }
0203     
0204     /* the buffer shall be equal */
0205     
0206     assert_equal(memcmp(buf1, buf2, sizeof(buf1)), 0);
0207     
0208 }
0209 
0210 /**
0211  * Test of FAST add test
0212  * Some field exists before and after 
0213  */
0214 Ensure(test_Baddfast2)
0215 {
0216     Bfld_loc_info_t state;
0217     int i;
0218     char str[16];
0219     char buf1[56000];
0220     UBFH *p_ub1 = (UBFH *)buf1;
0221     
0222     char buf2[56000];
0223     UBFH *p_ub2 = (UBFH *)buf2;
0224 
0225     
0226     memset(buf1, 0, sizeof(buf1));
0227     memset(buf2, 0, sizeof(buf2));
0228     
0229     assert_equal(Binit(p_ub1, sizeof(buf1)), EXSUCCEED);
0230     assert_equal(Binit(p_ub2, sizeof(buf2)), EXSUCCEED);
0231     
0232     
0233     
0234     assert_equal(Badd(p_ub1, T_STRING_2_FLD, "HELLO", 0), EXSUCCEED);
0235     assert_equal(Badd(p_ub2, T_STRING_2_FLD, "HELLO", 0), EXSUCCEED);
0236     
0237     assert_equal(CBadd(p_ub1, T_SHORT_FLD, "14", 0, BFLD_STRING), EXSUCCEED);
0238     assert_equal(CBadd(p_ub2, T_SHORT_FLD, "14", 0, BFLD_STRING), EXSUCCEED);
0239     
0240     assert_equal(CBadd(p_ub1, T_CARRAY_FLD, "WORLD", 0, BFLD_STRING), EXSUCCEED);
0241     assert_equal(CBadd(p_ub2, T_CARRAY_FLD, "WORLD", 0, BFLD_STRING), EXSUCCEED);
0242     
0243     /* now perform fast add */
0244     memset(&state, 0, sizeof(state));
0245     
0246     for (i=0; i<100; i++)
0247     {
0248     snprintf(str, sizeof(str), "%i", i);
0249     assert_equal(Baddfast(p_ub1, T_STRING_FLD, str, 0, &state), EXSUCCEED);
0250         assert_equal(Badd(p_ub2, T_STRING_FLD, str, 0), EXSUCCEED);
0251     }
0252     
0253     /* the buffer shall be equal */
0254     
0255     assert_equal(memcmp(buf1, buf2, sizeof(buf1)), 0);
0256     
0257     
0258     /* should fail if state is NULL */
0259     
0260     assert_equal(Baddfast(p_ub1, T_STRING_FLD, str, 0, NULL), EXFAIL);
0261     assert_equal(Berror, BEINVAL);
0262     
0263 }
0264 
0265 /**
0266  * Common suite entry
0267  * @return
0268  */
0269 TestSuite *ubf_Badd_tests(void)
0270 {
0271     TestSuite *suite = create_test_suite();
0272 
0273     set_setup(suite, basic_setup1);
0274     set_teardown(suite, basic_teardown1);
0275 
0276     add_test(suite, test_Badd_str);
0277     add_test(suite, test_Baddfast1);
0278     add_test(suite, test_Baddfast2);
0279 
0280     return suite;
0281 }