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