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