Back to home page

Enduro/X

 
 

    


0001 /* 
0002 **
0003 ** @file test_genbuf.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 #include "fdatatype.h"
0041 
0042 /**
0043  * Bcpy tests.
0044  */
0045 Ensure(test_Bcpy)
0046 {
0047     char buf1[550];
0048     char buf2[480];
0049     char data_buf[480];
0050 
0051     BFLDLEN len;
0052     UBFH *p_ub_src = (UBFH *)buf1;
0053     UBFH *p_ub_dst = (UBFH *)buf2;
0054 
0055     assert_equal(Binit(p_ub_src, sizeof(buf1)), EXSUCCEED);
0056     assert_equal(Binit(p_ub_dst, sizeof(buf2)), EXSUCCEED);
0057 
0058     /* Having no data it should copy OK */
0059     assert_equal(Bcpy(p_ub_dst, p_ub_src), EXSUCCEED);
0060     /* Set up some 300 bytes */
0061     len = sizeof(buf2);
0062     assert_equal(CBchg(p_ub_src, T_CARRAY_FLD, 0, data_buf, sizeof(data_buf), BFLD_CARRAY), EXSUCCEED);
0063 
0064     /* Now copy should fail, because of no space in buffer! */
0065     assert_equal_with_message(Bcpy(p_ub_dst, p_ub_src), EXFAIL, "Dest have no space!");
0066 }
0067 
0068 /**
0069  * Test user type conversation function
0070  * which dynamically allocates the buffer.
0071  */
0072 Ensure(test_btypcvt)
0073 {
0074     BFLDLEN len;
0075     short s=-11;
0076     long l=111;
0077     char c='3';
0078     float f=1.33341;
0079     double d=5547;
0080     char *p;
0081 
0082     /* Convert to short validation */
0083     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)&s, BFLD_SHORT, 0)), NULL);
0084     assert_equal(*((short *)p), -11);
0085     free(p);
0086     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)&l, BFLD_LONG, 0)), NULL);
0087     assert_equal(*((short *)p), 111);
0088     free(p);
0089     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)&c, BFLD_CHAR, 0)), NULL);
0090     assert_equal(*((short *)p), 51);
0091     free(p);
0092     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)&f, BFLD_FLOAT, 0)), NULL);
0093     assert_equal(*((short *)p), 1);
0094     free(p);
0095     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0096     assert_equal(*((short *)p), 5547);
0097     free(p);
0098     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)"12701", BFLD_STRING, 0)), NULL);
0099     assert_equal(*((short *)p), 12701);
0100     assert_equal(len, 2);
0101     free(p);
0102     assert_not_equal((p=Btypcvt(&len, BFLD_SHORT, (char *)"12801", BFLD_CARRAY, 5)), NULL);
0103     assert_equal(*((short *)p), 12801);
0104     free(p);
0105 
0106     /* Convert to long validation */
0107     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)&s, BFLD_SHORT, 0)), NULL);
0108     assert_equal(*((long *)p), -11);
0109     free(p);
0110     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)&l, BFLD_LONG, 0)), NULL);
0111     assert_equal(*((long *)p), 111);
0112     free(p);
0113     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)&c, BFLD_CHAR, 0)), NULL);
0114     assert_equal(*((long *)p), 51);
0115     free(p);
0116     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)&f, BFLD_FLOAT, 0)), NULL);
0117     assert_equal(*((long *)p), 1);
0118     free(p);
0119     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0120     assert_equal(*((long *)p), 5547);
0121     free(p);
0122     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)"12701123", BFLD_STRING, 0)), NULL);
0123     assert_equal(*((long *)p), 12701123);
0124     /* fix for 32bit system */
0125     if (sizeof(long)==8)
0126         assert_equal(len, 8);
0127     else
0128         assert_equal(len, 4);
0129 
0130     free(p);
0131     assert_not_equal((p=Btypcvt(&len, BFLD_LONG, (char *)"12801123", BFLD_CARRAY, 8)), NULL);
0132     assert_equal(*((long *)p), 12801123);
0133     free(p);
0134 
0135     /* Convert to char validation */
0136     s=111;
0137     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)&s, BFLD_SHORT, 0)), NULL);
0138     assert_equal(*((char *)p), 'o');
0139     free(p);
0140     s=-11;
0141     
0142     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)&l, BFLD_LONG, 0)), NULL);
0143     assert_equal(*((char *)p), 'o');
0144     free(p);
0145     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)&c, BFLD_CHAR, 0)), NULL);
0146     assert_equal(*((char *)p), '3');
0147     free(p);
0148     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)&f, BFLD_FLOAT, 0)), NULL);
0149     assert_equal(*((char *)p), 1);
0150     free(p);
0151     d=47;
0152     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0153     assert_equal(*((char *)p), '/');
0154     d=5547;
0155     free(p);
0156     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)"abc", BFLD_STRING, 0)), NULL);
0157     assert_equal(*((char *)p), 'a');
0158     assert_equal(len, 1);
0159     free(p);
0160     assert_not_equal((p=Btypcvt(&len, BFLD_CHAR, (char *)"cde", BFLD_CARRAY, 8)), NULL);
0161     assert_equal(*((char *)p), 'c');
0162     free(p);
0163 
0164 
0165     /* Convert to float validation */
0166     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)&s, BFLD_SHORT, 0)), NULL);
0167     assert_double_equal(*((float *)p), -11);
0168     free(p);
0169     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)&l, BFLD_LONG, 0)), NULL);
0170     assert_double_equal(*((float *)p), 111);
0171     free(p);
0172     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)&c, BFLD_CHAR, 0)), NULL);
0173     assert_double_equal(*((float *)p), 51);
0174     free(p);
0175     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)&f, BFLD_FLOAT, 0)), NULL);
0176     assert_double_equal(*((float *)p), 1.33341);
0177     free(p);
0178     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0179     assert_double_equal(*((float *)p), 5547);
0180     free(p);
0181     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)"12701.1", BFLD_STRING, 0)), NULL);
0182     assert_double_equal(*((float *)p), 12701.1);
0183     assert_equal(len, 4);
0184     free(p);
0185     assert_not_equal((p=Btypcvt(&len, BFLD_FLOAT, (char *)"128.1", BFLD_CARRAY, 5)), NULL);
0186     assert_double_equal(*((float *)p), 128.1);
0187     free(p);
0188 
0189 
0190     /* Convert to double validation */
0191     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)&s, BFLD_SHORT, 0)), NULL);
0192     assert_double_equal(*((double *)p), -11);
0193     free(p);
0194     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)&l, BFLD_LONG, 0)), NULL);
0195     assert_double_equal(*((double *)p), 111);
0196     free(p);
0197     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)&c, BFLD_CHAR, 0)), NULL);
0198     assert_double_equal(*((double *)p), 51);
0199     free(p);
0200     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)&f, BFLD_FLOAT, 0)), NULL);
0201     assert_double_equal(*((double *)p), 1.33341);
0202     free(p);
0203     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0204     assert_double_equal(*((double *)p), 5547);
0205     free(p);
0206     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)"12701.1", BFLD_STRING, 0)), NULL);
0207     assert_double_equal(*((double *)p), 12701.1);
0208     assert_equal(len, 8);
0209     free(p);
0210     assert_not_equal((p=Btypcvt(&len, BFLD_DOUBLE, (char *)"128.1", BFLD_CARRAY, 5)), NULL);
0211     assert_double_equal(*((double *)p), 128.1);
0212     free(p);
0213 
0214 
0215     /* Convert to string validation */
0216     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)&s, BFLD_SHORT, 0)), "-11");
0217     free(p);
0218     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)&l, BFLD_LONG, 0)), "111");
0219     free(p);
0220     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)&c, BFLD_CHAR, 0)), "3");
0221     free(p);
0222     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)&f, BFLD_FLOAT, 0)), "1.33341");
0223     free(p);
0224     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)&d, BFLD_DOUBLE, 0)), "5547.000000");
0225     free(p);
0226     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)"TEST STRING", BFLD_STRING, 0)), "TEST STRING");
0227     free(p);
0228     assert_string_equal((p=Btypcvt(&len, BFLD_STRING, (char *)"TEST CARRAY", BFLD_CARRAY, 11)), "TEST CARRAY");
0229     free(p);
0230     
0231     /* Convert to string validation */
0232     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)&s, BFLD_SHORT, 0)), NULL);
0233     assert_equal(strncmp(p, "-11", 3), 0);
0234     free(p);
0235     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)&l, BFLD_LONG, 0)), NULL);
0236     assert_equal(strncmp(p, "111", 3), 0);
0237     free(p);
0238     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)&c, BFLD_CHAR, 0)), NULL);
0239     assert_equal(strncmp(p, "3", 1), 0);
0240     free(p);
0241     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)&f, BFLD_FLOAT, 0)), NULL);
0242     assert_equal(strncmp(p, "1.33341", 7), 0);
0243     free(p);
0244     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)&d, BFLD_DOUBLE, 0)), NULL);
0245     assert_equal(strncmp(p, "5547.000000", 11), 0);
0246     free(p);
0247     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)"TEST STRING", BFLD_STRING, 0)), NULL);
0248     assert_equal(strncmp(p, "TEST STRING", 11), 0);
0249     free(p);
0250     assert_not_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)"TEST CARRAY", BFLD_CARRAY, 11)), NULL);
0251     assert_equal(strncmp(p, "TEST CARRAY", 11), 0);
0252     assert_equal(len, 11);
0253     free(p);
0254 
0255     /* test invalid types */
0256     assert_equal((p=Btypcvt(&len, 11, (char *)"TEST CARRAY", BFLD_CARRAY, 11)), NULL);
0257     assert_equal(Berror, BTYPERR);
0258     assert_equal((p=Btypcvt(&len, BFLD_CARRAY, (char *)"TEST CARRAY", 12, 11)), NULL);
0259     assert_equal(Berror, BTYPERR);
0260     assert_equal((p=Btypcvt(&len, BFLD_CARRAY, NULL, 12, 11)), NULL);
0261     assert_equal(Berror, BEINVAL);
0262 }
0263 
0264 /**
0265  * Test data structures by it self
0266  */
0267 Ensure(ubf_test_struct)
0268 {
0269     /* test data structures */
0270     assert_equal(sizeof(UBF_header_t), 48);
0271 }
0272 
0273 /**
0274  * Common suite entry
0275  * @return
0276  */
0277 TestSuite *ubf_genbuf_tests(void)
0278 {
0279     TestSuite *suite = create_test_suite();
0280 /*
0281     setup_(suite, basic_setup1);
0282     teardown_(suite, basic_teardown1);
0283 */
0284     add_test(suite, test_Bcpy);
0285     add_test(suite, test_btypcvt);
0286     add_test(suite, ubf_test_struct);
0287 
0288     return suite;
0289 }