Back to home page

Enduro/X

 
 

    


0001 /* 
0002 ** Contains routines for testing find kind functions
0003 **
0004 ** @file test_find.c
0005 ** 
0006 ** -----------------------------------------------------------------------------
0007 ** Enduro/X Middleware Platform for Distributed Transaction Processing
0008 ** Copyright (C) 2015, 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 2 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 void load_find_test_data(UBFH *p_ub)
0044 {
0045     short s = 88;
0046     long l = -1021;
0047     char c = 'c';
0048     float f = 17.31;
0049     double d = 12312.1111;
0050     char carr[] = "CARRAY1 TEST STRING DATA";
0051     BFLDLEN len = strlen(carr);
0052 
0053     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&s, 0), EXSUCCEED);
0054     assert_equal(Bchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0), EXSUCCEED);
0055     assert_equal(Bchg(p_ub, T_CHAR_FLD, 0, (char *)&c, 0), EXSUCCEED);
0056     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 0, (char *)&f, 0), EXSUCCEED);
0057     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 0, (char *)&d, 0), EXSUCCEED);
0058     assert_equal(Bchg(p_ub, T_STRING_FLD, 0, (char *)"TEST STR VAL", 0), EXSUCCEED);
0059     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 0, (char *)carr, len), EXSUCCEED);
0060 
0061     /* Make second copy of field data (another for not equal test)*/
0062     s = 88;
0063     l = -1021;
0064     c = '.';
0065     f = 17.31;
0066     d = 12312.1111;
0067     carr[0] = 'Y';
0068     len = strlen(carr);
0069 
0070     assert_equal(Bchg(p_ub, T_SHORT_FLD, 1, (char *)&s, 0), EXSUCCEED);
0071     assert_equal(Bchg(p_ub, T_LONG_FLD, 1, (char *)&l, 0), EXSUCCEED);
0072     assert_equal(Bchg(p_ub, T_CHAR_FLD, 1, (char *)&c, 0), EXSUCCEED);
0073     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 1, (char *)&f, 0), EXSUCCEED);
0074     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 1, (char *)&d, 0), EXSUCCEED);
0075     assert_equal(Bchg(p_ub, T_STRING_FLD, 1, (char *)"TEST STRING ARRAY2", 0), EXSUCCEED);
0076     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 1, (char *)carr, len), EXSUCCEED);
0077 
0078     l = 888;
0079     assert_equal(Bchg(p_ub, T_LONG_FLD, 4, (char *)&l, 0), EXSUCCEED);
0080 
0081     s = 212;
0082     l = 212;
0083     c = 'b';
0084     f = 12127;
0085     d = 1231232.1;
0086     carr[0] = 'X';
0087     assert_equal(Bchg(p_ub, T_SHORT_2_FLD, 0, (char *)&s, 0), EXSUCCEED);
0088     assert_equal(Bchg(p_ub, T_LONG_2_FLD, 0, (char *)&l, 0), EXSUCCEED);
0089     assert_equal(Bchg(p_ub, T_CHAR_2_FLD, 0, (char *)&c, 0), EXSUCCEED);
0090     assert_equal(Bchg(p_ub, T_FLOAT_2_FLD, 0, (char *)&f, 0), EXSUCCEED);
0091     assert_equal(Bchg(p_ub, T_DOUBLE_2_FLD, 0, (char *)&d, 0), EXSUCCEED);
0092     assert_equal(Bchg(p_ub, T_STRING_2_FLD, 0, (char *)"XTEST STR VAL", 0), EXSUCCEED);
0093     assert_equal(Bchg(p_ub, T_CARRAY_2_FLD, 0, (char *)carr, len), EXSUCCEED);
0094 }
0095 
0096 /**
0097  * Test data used for CBfindocc
0098  * @param p_ub
0099  */
0100 void load_bfindocc_test_data(UBFH *p_ub)
0101 {
0102     short s = 881;
0103     long l = -1011;
0104     char c = 'a';
0105     float f = 1.31;
0106     double d = 1312.1111;
0107     char carr[] = "ZARRAY1 TEST STRING DATA";
0108     BFLDLEN len = strlen(carr);
0109 
0110     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&s, 0), EXSUCCEED);
0111     assert_equal(Bchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0), EXSUCCEED);
0112     assert_equal(Bchg(p_ub, T_CHAR_FLD, 0, (char *)&c, 0), EXSUCCEED);
0113     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 0, (char *)&f, 0), EXSUCCEED);
0114     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 0, (char *)&d, 0), EXSUCCEED);
0115     assert_equal(Bchg(p_ub, T_STRING_FLD, 0, (char *)"TEST STR VAL", 0), EXSUCCEED);
0116     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 0, (char *)carr, len), EXSUCCEED);
0117 
0118     /* Make second copy of field data (another for not equal test)*/
0119     s = 88;
0120     l = -1021;
0121     c = '.';
0122     f = 17.31;
0123     d = 12312.1111;
0124     carr[0] = '\0';
0125     len = 24;
0126 
0127     assert_equal(Bchg(p_ub, T_SHORT_FLD, 1, (char *)&s, 0), EXSUCCEED);
0128     assert_equal(Bchg(p_ub, T_LONG_FLD, 1, (char *)&l, 0), EXSUCCEED);
0129     assert_equal(Bchg(p_ub, T_CHAR_FLD, 1, (char *)&c, 0), EXSUCCEED);
0130     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 1, (char *)&f, 0), EXSUCCEED);
0131     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 1, (char *)&d, 0), EXSUCCEED);
0132     assert_equal(Bchg(p_ub, T_STRING_FLD, 1, (char *)"TEST STRING ARRAY2", 0), EXSUCCEED);
0133     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 1, (char *)carr, len), EXSUCCEED);
0134 
0135     l = 888;
0136     assert_equal(Bchg(p_ub, T_LONG_FLD, 4, (char *)&l, 0), EXSUCCEED);
0137 
0138     s = 212;
0139     l = 212;
0140     c = 'b';
0141     f = 12127;
0142     d = 1231232.1;
0143     carr[0] = 'X';
0144     assert_equal(Bchg(p_ub, T_SHORT_2_FLD, 0, (char *)&s, 0), EXSUCCEED);
0145     assert_equal(Bchg(p_ub, T_LONG_2_FLD, 0, (char *)&l, 0), EXSUCCEED);
0146     assert_equal(Bchg(p_ub, T_CHAR_2_FLD, 0, (char *)&c, 0), EXSUCCEED);
0147     assert_equal(Bchg(p_ub, T_FLOAT_2_FLD, 0, (char *)&f, 0), EXSUCCEED);
0148     assert_equal(Bchg(p_ub, T_DOUBLE_2_FLD, 0, (char *)&d, 0), EXSUCCEED);
0149     assert_equal(Bchg(p_ub, T_STRING_2_FLD, 0, (char *)"XTEST STR VAL", 0), EXSUCCEED);
0150     assert_equal(Bchg(p_ub, T_CARRAY_2_FLD, 0, (char *)carr, len), EXSUCCEED);
0151 }
0152 
0153 
0154 /**
0155  * Test data used for Bfindlast
0156  * @param p_ub
0157  */
0158 void load_bfindlast_test_data(UBFH *p_ub)
0159 {
0160     short s = 88;
0161     long l = -1021;
0162     char c = 'c';
0163     float f = 17.31;
0164     double d = 12312.1111;
0165     char carr[] = "CARRAY1 TEST STRING DATA";
0166     BFLDLEN len = strlen(carr);
0167 
0168     assert_equal(Bchg(p_ub, T_SHORT_FLD, 0, (char *)&s, 0), EXSUCCEED);
0169     assert_equal(Bchg(p_ub, T_LONG_FLD, 1, (char *)&l, 0), EXSUCCEED);
0170     assert_equal(Bchg(p_ub, T_CHAR_FLD, 2, (char *)&c, 0), EXSUCCEED);
0171     assert_equal(Bchg(p_ub, T_FLOAT_FLD, 3, (char *)&f, 0), EXSUCCEED);
0172     assert_equal(Bchg(p_ub, T_DOUBLE_FLD, 4, (char *)&d, 0), EXSUCCEED);
0173     assert_equal(Bchg(p_ub, T_STRING_FLD, 5, (char *)"TEST STR VAL", 0), EXSUCCEED);
0174     assert_equal(Bchg(p_ub, T_CARRAY_FLD, 6, (char *)carr, len), EXSUCCEED);
0175 
0176 }
0177 
0178 /**
0179  * This simply reads all field and adds them to another buffer, then do compare
0180  */
0181 Ensure(test_cbfind)
0182 {
0183     char fb[1048];
0184     UBFH *p_ub = (UBFH *)fb;
0185     short *s;
0186     long *l;
0187     char *c;
0188     float *f;
0189     double *d;
0190     char *str;
0191     char *carr;
0192     BFLDLEN len=0;
0193 
0194     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0195     load_find_test_data(p_ub);
0196 
0197     /* Test as short */
0198     assert_equal(*(s=(short *)CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_SHORT)), 88);
0199     assert_equal(*(s=(short *)CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_SHORT)), -1021);
0200     assert_equal(*(s=(short *)CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_SHORT)), 99);
0201     assert_equal(*(s=(short *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_SHORT)), 17);
0202     assert_equal(*(s=(short *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_SHORT)), 12312);
0203     assert_equal(*(s=(short *)CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_SHORT)), 0);
0204     assert_equal(*(s=(short *)CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_SHORT)), 0);
0205 
0206     /* Test as long */
0207     assert_equal(*(l=(long *)CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_LONG)), 88);
0208     assert_equal(*(l=(long *)CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_LONG)), -1021);
0209     assert_equal(*(l=(long *)CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_LONG)), 99);
0210     assert_equal(*(l=(long *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_LONG)), 17);
0211     assert_equal(*(l=(long *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_LONG)), 12312);
0212     assert_equal(*(l=(long *)CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_LONG)), 0);
0213     assert_equal(*(l=(long *)CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_LONG)), 0);
0214 
0215     /* Test as char */
0216     assert_equal(*(c=(char *)CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_CHAR)), 'X');
0217     assert_equal(*(c=(char *)CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_CHAR)), 3); /* may be incorrect due to data size*/
0218     assert_equal(*(c=(char *)CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_CHAR)), 'c');
0219     assert_equal(*(c=(char *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_CHAR)), 17);
0220     assert_equal(*(c=(char *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_CHAR)), 24); /* May be incorrect dute to data size*/
0221     assert_equal(*(c=(char *)CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_CHAR)), 'T');
0222     assert_equal(*(c=(char *)CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_CHAR)), 'C');
0223 
0224     /* Test as float */
0225     assert_double_equal(*(f=(float *)CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_FLOAT)), 88);
0226     assert_double_equal(*(f=(float *)CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_FLOAT)), -1021);
0227     assert_double_equal(*(f=(float *)CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_FLOAT)), 99);
0228     assert_double_equal(*(f=(float *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_FLOAT)), 17.31);
0229     assert_double_equal(*(f=(float *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_FLOAT)), 12312.1111);
0230     assert_double_equal(*(f=(float *)CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_FLOAT)), 0);
0231     assert_double_equal(*(f=(float *)CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_FLOAT)), 0);
0232 
0233 
0234     /* Test as double */
0235     assert_double_equal(*(d=(double *)CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_DOUBLE)), 88);
0236     assert_double_equal(*(d=(double *)CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_DOUBLE)), -1021);
0237     assert_double_equal(*(d=(double *)CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_DOUBLE)), 99);
0238     assert_double_equal(*(d=(double *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_DOUBLE)), 17.31);
0239     assert_double_equal(*(d=(double *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_DOUBLE)), 12312.1111);
0240     assert_double_equal(*(d=(double *)CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_DOUBLE)), 0);
0241     assert_double_equal(*(d=(double *)CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_DOUBLE)), 0);
0242 
0243     /* Test as string */
0244     assert_string_equal(CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_STRING), "88");
0245     assert_string_equal(CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_STRING), "-1021");
0246     assert_string_equal(CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_STRING), "c");
0247     assert_equal(strncmp(CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_STRING), "17.3", 4), 0);
0248     assert_equal(strncmp(CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_STRING), "12312.1111", 10), 0);
0249     assert_string_equal(CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_STRING), "TEST STR VAL");
0250     assert_string_equal(CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_STRING), "CARRAY1 TEST STRING DATA");
0251 
0252     /* Test as carray */
0253     assert_equal(strncmp(CBfind(p_ub, T_SHORT_FLD, 0, 0, BFLD_CARRAY), "88", 2), 0);
0254     assert_equal(strncmp(CBfind(p_ub, T_LONG_FLD, 0, 0, BFLD_CARRAY), "-1021", 5), 0);
0255     assert_equal(strncmp(CBfind(p_ub, T_CHAR_FLD, 0, 0, BFLD_CARRAY), "c", 1), 0);
0256     assert_equal(strncmp(CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_CARRAY), "17.3", 4), 0);
0257     assert_equal(strncmp(CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_CARRAY), "12312.1111", 10), 0);
0258     assert_equal(strncmp(CBfind(p_ub, T_STRING_FLD, 0, 0, BFLD_CARRAY), "TEST STR VAL", 12), 0);
0259     assert_equal(strncmp(CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_CARRAY), "CARRAY1 TEST STRING DATA", 24), 0);
0260 
0261     /* Now test the thing that we have different pointers for each of the data type
0262      * also fields will corss match their types.
0263      */
0264     assert_not_equal((s=(short *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_SHORT)), NULL);
0265     assert_not_equal((l=(long *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_LONG)), NULL);
0266     assert_not_equal((c=(char *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_CHAR)), NULL);
0267     assert_not_equal((f=(float *)CBfind(p_ub, T_DOUBLE_FLD, 0, 0, BFLD_FLOAT)), NULL);
0268     assert_not_equal((d=(double *)CBfind(p_ub, T_FLOAT_FLD, 0, 0, BFLD_DOUBLE)), NULL);
0269     assert_not_equal((str=CBfind(p_ub, T_CARRAY_FLD, 0, 0, BFLD_STRING)), NULL);
0270     assert_not_equal((carr=CBfind(p_ub, T_STRING_FLD, 0, &len, BFLD_CARRAY)), NULL);
0271     
0272     /* Now compare the values */
0273     assert_equal(*s, 17);
0274     assert_equal(*l, 12312);
0275     assert_equal(*c, 17);
0276     assert_double_equal(*f, 12312.1111);
0277     assert_double_equal(*d, 17.31);
0278     assert_string_equal(str, "CARRAY1 TEST STRING DATA");
0279     assert_equal(len, 12);
0280     assert_equal(strncmp(carr, "TEST STR VAL", len), 0);
0281 
0282     /* Now test the error case, when field is not found? */
0283     assert_equal(CBfind(p_ub, T_FLOAT_FLD, 10, 0, BFLD_SHORT), NULL);
0284     assert_equal(Berror, BNOTPRES);
0285 
0286     /* try to get data from other occurrance */
0287     assert_double_equal(*(d=(double *)CBfind(p_ub, T_LONG_FLD, 4, 0, BFLD_DOUBLE)), 888);
0288 
0289     /* Now play with big buffer data */
0290     assert_equal(Bchg(p_ub, T_CARRAY_2_FLD, 4, BIG_TEST_STRING, strlen(BIG_TEST_STRING)),EXSUCCEED);
0291     /* now match the string */
0292     assert_string_equal((str=CBfind(p_ub, T_CARRAY_2_FLD, 4, 0, BFLD_STRING)), BIG_TEST_STRING);
0293 
0294 }
0295 
0296 /**
0297  * Get occurrance out from value.
0298  */
0299 Ensure(test_bfindocc)
0300 {
0301     char fb[1024];
0302     UBFH *p_ub = (UBFH *)fb;
0303     short s;
0304     long l;
0305     char c;
0306     float f;
0307     double d;
0308     char *str;
0309     char *carr;
0310     BFLDLEN len=0;
0311 
0312 
0313     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0314     load_bfindocc_test_data(p_ub);
0315 
0316     s = 88;
0317     l = -1021;
0318     c = '.';
0319     f = 17.31;
0320     d = 12312.1111;
0321     str = "TEST STRING ARRAY2";
0322     carr = "\0ARRAY1 TEST STRING DATA";
0323     assert_equal(Bfindocc(p_ub, T_SHORT_FLD, (char*) &s, 0),1);
0324     assert_equal(Bfindocc(p_ub, T_LONG_FLD, (char*) &l, 0),1);
0325     assert_equal(Bfindocc(p_ub, T_CHAR_FLD, (char*) &c, 0),1);
0326     assert_equal(Bfindocc(p_ub, T_FLOAT_FLD, (char*) &f, 0),1);
0327     assert_equal(Bfindocc(p_ub, T_DOUBLE_FLD, (char*) &d, 0),1);
0328     assert_equal(Bfindocc(p_ub, T_STRING_FLD, (char*) str, 0),1);
0329     assert_equal(Bfindocc(p_ub, T_CARRAY_FLD, (char*) carr, 24),1);
0330     /* Now test first occurrance */
0331     s = 881;
0332     l = -1011;
0333     c = 'a';
0334     f = 1.31;
0335     d = 1312.1111;
0336     str = "TEST STR VAL";
0337     carr = "ZARRAY1 TEST STRING DATA";
0338 
0339     assert_equal(Bfindocc(p_ub, T_SHORT_FLD, (char*) &s, 0),0);
0340     assert_equal(Bfindocc(p_ub, T_LONG_FLD, (char*) &l, 0),0);
0341     assert_equal(Bfindocc(p_ub, T_CHAR_FLD, (char*) &c, 0),0);
0342     assert_equal(Bfindocc(p_ub, T_FLOAT_FLD, (char*) &f, 0),0);
0343     assert_equal(Bfindocc(p_ub, T_DOUBLE_FLD, (char*) &d, 0),0);
0344     assert_equal(Bfindocc(p_ub, T_STRING_FLD, (char*) str, 0),0);
0345     assert_equal(Bfindocc(p_ub, T_CARRAY_FLD, (char*) carr, 24),0);
0346 
0347     /* Test regexp version */
0348     assert_equal(Bchg(p_ub, T_STRING_FLD, 10, "A", 0), EXSUCCEED);
0349     assert_equal(Bchg(p_ub, T_STRING_FLD, 11, "ABC", 0), EXSUCCEED);
0350     assert_equal(Bchg(p_ub, T_STRING_FLD, 20, "TESTEST", 0), EXSUCCEED);
0351     assert_equal(Bfindocc(p_ub, T_STRING_FLD, "B|A", 1), 10);
0352     assert_equal(Bfindocc(p_ub, T_STRING_FLD, "A.C", 1), 11);
0353     assert_equal(Bfindocc(p_ub, T_STRING_FLD, "TESTEST(|\\(|", 1), -1);
0354     assert_equal(Bfindocc(p_ub, T_STRING_FLD, "TES....", 1), 20);
0355 }
0356 
0357 /**
0358  * Get occurrance out from value (from user converted value)
0359  */
0360 Ensure(test_cbfindocc)
0361 {
0362     char fb[2048];
0363     UBFH *p_ub = (UBFH *)fb;
0364     short s;
0365     long l;
0366     char c;
0367     float f;
0368     double d;
0369     char *str;
0370     char *carr;
0371     BFLDLEN len=0;
0372 
0373 
0374     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0375     load_find_test_data(p_ub);
0376     
0377     /* Override some values for common test */
0378     assert_equal(CBchg(p_ub, T_LONG_FLD, 100, "251", 0, BFLD_STRING), EXSUCCEED);
0379     assert_equal(CBchg(p_ub, T_FLOAT_FLD, 12, "102", 0, BFLD_STRING), EXSUCCEED);
0380     assert_equal(CBchg(p_ub, T_DOUBLE_FLD, 13, "99", 0, BFLD_STRING), EXSUCCEED);
0381     assert_equal(CBchg(p_ub, T_STRING_FLD, 14, "55", 0, BFLD_STRING), EXSUCCEED);
0382     assert_equal(CBchg(p_ub, T_CARRAY_FLD, 15, "66", 0, BFLD_STRING), EXSUCCEED);
0383     
0384     /* Test as short */
0385     s=88;
0386     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, (char *)&s, 0, BFLD_SHORT), 0);
0387     s=251;
0388     assert_equal(CBfindocc(p_ub, T_LONG_FLD, (char *)&s, 0, BFLD_SHORT), 100);
0389     s=99;
0390     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, (char *)&s, 0, BFLD_SHORT), 0);
0391     s=102;
0392     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, (char *)&s, 0, BFLD_SHORT), 12);
0393     s=99;
0394     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, (char *)&s, 0, BFLD_SHORT), 13);
0395     s=55;
0396     assert_equal(CBfindocc(p_ub, T_STRING_FLD, (char *)&s, 0, BFLD_SHORT), 14);
0397     s=66;
0398     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, (char *)&s, 0, BFLD_SHORT), 15);
0399 
0400 
0401     /* Test as long */
0402     l=88;
0403     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, (char *)&l, 0, BFLD_LONG), 0);
0404     l=251;
0405     assert_equal(CBfindocc(p_ub, T_LONG_FLD, (char *)&l, 0, BFLD_LONG), 100);
0406     l=99;
0407     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, (char *)&l, 0, BFLD_LONG), 0);
0408     l=102;
0409     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, (char *)&l, 0, BFLD_LONG), 12);
0410     l=99;
0411     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, (char *)&l, 0, BFLD_LONG), 13);
0412     l=55;
0413     assert_equal(CBfindocc(p_ub, T_STRING_FLD, (char *)&l, 0, BFLD_LONG), 14);
0414     l=66;
0415     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, (char *)&l, 0, BFLD_LONG), 15);
0416 
0417     /* Test as char */
0418     c=88;
0419     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, (char *)&c, 0, BFLD_CHAR), 0);
0420     c=251;
0421     assert_equal(CBfindocc(p_ub, T_LONG_FLD, (char *)&c, 0, BFLD_CHAR), 100);
0422     c=99;
0423     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, (char *)&c, 0, BFLD_CHAR), 0);
0424     c=102;
0425     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, (char *)&c, 0, BFLD_CHAR), 12);
0426     c=99;
0427     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, (char *)&c, 0, BFLD_CHAR), 13);
0428 
0429     assert_equal(CBchg(p_ub, T_STRING_FLD, 14, "7", 0, BFLD_STRING), EXSUCCEED);
0430     assert_equal(CBchg(p_ub, T_CARRAY_FLD, 15, "A", 0, BFLD_STRING), EXSUCCEED);
0431 
0432     c=55;
0433     assert_equal(CBfindocc(p_ub, T_STRING_FLD, (char *)&c, 0, BFLD_CHAR), 14);
0434     c=65;
0435     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, (char *)&c, 0, BFLD_CHAR), 15);
0436 
0437     assert_equal(CBchg(p_ub, T_STRING_FLD, 14, "55", 0, BFLD_STRING), EXSUCCEED);
0438     assert_equal(CBchg(p_ub, T_CARRAY_FLD, 15, "66", 0, BFLD_STRING), EXSUCCEED);
0439 
0440     /* Test as float */
0441     f=88;
0442     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, (char *)&f, 0, BFLD_FLOAT), 0);
0443     f=251;
0444     assert_equal(CBfindocc(p_ub, T_LONG_FLD, (char *)&f, 0, BFLD_FLOAT), 100);
0445     f=99;
0446     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, (char *)&f, 0, BFLD_FLOAT), 0);
0447     f=102;
0448     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, (char *)&f, 0, BFLD_FLOAT), 12);
0449     f=99;
0450     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, (char *)&f, 0, BFLD_FLOAT), 13);
0451     
0452     /* Due to string as float value after the comma, we cannot find such values */
0453     f=55;
0454     assert_equal(CBfindocc(p_ub, T_STRING_FLD, (char *)&f, 0, BFLD_FLOAT), -1);
0455     f=66;
0456     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, (char *)&f, 0, BFLD_FLOAT), -1);
0457 
0458 
0459     /* Test as double */
0460     d=88;
0461     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, (char *)&d, 0, BFLD_DOUBLE), 0);
0462     d=251;
0463     assert_equal(CBfindocc(p_ub, T_LONG_FLD, (char *)&d, 0, BFLD_DOUBLE), 100);
0464     d=99;
0465     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, (char *)&d, 0, BFLD_DOUBLE), 0);
0466     d=102;
0467     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, (char *)&d, 0, BFLD_DOUBLE), 12);
0468     d=99;
0469     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, (char *)&d, 0, BFLD_DOUBLE), 13);
0470     
0471     /* Due to string as float value after the comma, we cannot find such values */
0472     d=55;
0473     assert_equal(CBfindocc(p_ub, T_STRING_FLD, (char *)&d, 0, BFLD_DOUBLE), -1);
0474     d=66;
0475     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, (char *)&d, 0, BFLD_DOUBLE), -1);
0476     
0477     /* Test as string */
0478     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, "88", 0, BFLD_STRING), 0);
0479     assert_equal(CBfindocc(p_ub, T_LONG_FLD, "251", 0, BFLD_STRING), 100);
0480     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, "c", 0, BFLD_STRING), 0);
0481     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, "102", 0, BFLD_STRING), 12);
0482     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, "99", 0, BFLD_STRING), 13);
0483     assert_equal(CBfindocc(p_ub, T_STRING_FLD, "55", 0, BFLD_STRING), 14);
0484     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, "66", 0, BFLD_STRING), 15);
0485     
0486     /* Test as carray */
0487     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, "88", 2, BFLD_STRING), 0);
0488     assert_equal(CBfindocc(p_ub, T_LONG_FLD, "251", 3, BFLD_STRING), 100);
0489     assert_equal(CBfindocc(p_ub, T_CHAR_FLD, "c", 1, BFLD_STRING), 0);
0490     assert_equal(CBfindocc(p_ub, T_FLOAT_FLD, "102", 3, BFLD_STRING), 12);
0491     assert_equal(CBfindocc(p_ub, T_DOUBLE_FLD, "99", 2, BFLD_STRING), 13);
0492     assert_equal(CBfindocc(p_ub, T_STRING_FLD, "55", 2, BFLD_STRING), 14);
0493     assert_equal(CBfindocc(p_ub, T_CARRAY_FLD, "66", 2, BFLD_STRING), 15);
0494     
0495     /* Now test invalid argument */
0496     assert_equal(CBfindocc(p_ub, T_SHORT_FLD, "88", 2, 100), -1);
0497     assert_equal(Berror, BTYPERR);
0498 }
0499 
0500 /**
0501  * Test Bgetalloc
0502  */
0503 Ensure(test_bfindlast)
0504 {
0505     char fb[1024];
0506     UBFH *p_ub = (UBFH *)fb;
0507     short *s1;
0508     long *l1;
0509     char *c1;
0510     float *f1;
0511     double *d1;
0512     char *str1;
0513     char *carr1;
0514     BFLDLEN len=0;
0515     BFLDOCC occ=-1;
0516 
0517     assert_equal(Binit(p_ub, sizeof(fb)), EXSUCCEED);
0518     load_bfindlast_test_data(p_ub);
0519 
0520     /* Test as short */
0521     occ=-1;
0522     assert_not_equal((s1=(short *)Bfindlast(p_ub, T_SHORT_FLD, &occ, 0)), NULL);
0523     assert_equal(*s1, 88);
0524     assert_equal(occ, 0);
0525 
0526     /* Test as long */
0527     occ=-1;
0528     assert_not_equal((l1=(long *)Bfindlast(p_ub, T_LONG_FLD, &occ, 0)), NULL);
0529     assert_equal(*l1, -1021);
0530     assert_equal(occ, 1);
0531 
0532      /* Test as char */
0533     occ=-1;
0534     assert_not_equal((c1=(char *)Bfindlast(p_ub, T_CHAR_FLD, &occ, 0)), NULL);
0535     assert_equal(*c1, 'c');
0536     assert_equal(occ, 2);
0537 
0538     /* Test as float */
0539     occ=-1;
0540     assert_not_equal((f1=(float *)Bfindlast(p_ub, T_FLOAT_FLD, &occ, 0)), NULL);
0541     assert_double_equal(*f1, 17.31);
0542     assert_equal(occ, 3);
0543 
0544     /* Test as double */
0545     occ=-1;
0546     assert_not_equal((d1=(double *)Bfindlast(p_ub, T_DOUBLE_FLD, &occ, 0)), NULL);
0547     assert_double_equal(*d1, 12312.1111);
0548     assert_equal(occ, 4);
0549 
0550     /* Test as string */
0551     occ=-1;
0552     assert_not_equal((str1=Bfindlast(p_ub, T_STRING_FLD, &occ, 0)), NULL);
0553     assert_string_equal(str1, "TEST STR VAL");
0554     assert_equal(occ, 5);
0555 
0556     /* Test as carray */
0557     len = 1000;
0558     occ=-1;
0559     assert_not_equal((carr1=Bfindlast(p_ub, T_CARRAY_FLD, &occ, &len)), NULL);
0560     assert_equal(len, 24);
0561     assert_equal(strncmp(carr1, "CARRAY1 TEST STRING DATA", 24), 0);
0562     assert_equal(occ, 6);
0563     
0564     /* Test the case when data is not found! */
0565     occ=-1;
0566     assert_equal((str1=Bfindlast(p_ub, T_STRING_2_FLD, &occ, 0)), NULL);
0567     assert_equal(Berror, BNOTPRES);
0568     assert_equal(occ, -1);
0569 }
0570 
0571 TestSuite *ubf_find_tests(void)
0572 {
0573     TestSuite *suite = create_test_suite();
0574 
0575     add_test(suite, test_cbfind);
0576     add_test(suite, test_bfindocc);
0577     add_test(suite, test_cbfindocc);
0578     add_test(suite, test_bfindlast);
0579     
0580     return suite;
0581 }