Back to home page

Enduro/X

 
 

    


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