Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Linear hash testing
0003  *
0004  * @file test_nstd_lh.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-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
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 Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 
0035 /*---------------------------Includes-----------------------------------*/
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <cgreen/cgreen.h>
0039 #include <ubf.h>
0040 #include <ndrstandard.h>
0041 #include <string.h>
0042 #include <ndebug.h>
0043 #include <exbase64.h>
0044 #include <nstdutil.h>
0045 #include <fcntl.h>
0046 #include "test.fd.h"
0047 #include "ubfunit1.h"
0048 #include "xatmi.h"
0049 
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 
0053 #define TEST_INDEX(MEM, IDX) ((ndrx_lh_test_t*)(((char*)MEM)+(int)(sizeof(ndrx_lh_test_t)*IDX)))
0054 
0055 #define TEST_MEM_MAX    50 /**< how much we plan to store?             */
0056 #define TEST_RUN_MAX    200 /**< how much to loop over                */
0057 /*---------------------------Enums--------------------------------------*/
0058 /*---------------------------Typedefs-----------------------------------*/
0059 
0060 /**
0061  * Shared memory entry for service
0062  */
0063 typedef struct ndrx_lh_test ndrx_lh_test_t;
0064 struct ndrx_lh_test
0065 {
0066     int key;                            /**< Index key                  */
0067     int some_val;                       /**< Store some value           */
0068     short flags;                        /**< See NDRX_SVQ_MAP_STAT_*    */
0069 };
0070 
0071 /*---------------------------Globals------------------------------------*/
0072 /*---------------------------Statics------------------------------------*/
0073 /*---------------------------Prototypes---------------------------------*/
0074 
0075 /**
0076  * Generate hash key for value..
0077  * @param conf hash config
0078  * @param key_get key data
0079  * @param key_len key len (not used)
0080  * @return slot number within the array
0081  */
0082 exprivate int lhtest_key_hash(ndrx_lh_config_t *conf, void *key_get, size_t key_len)
0083 {
0084     int *p_key = (int *)key_get;
0085     return *p_key % conf->elmmax;
0086 }
0087 
0088 /**
0089  * Get debug
0090  * @param conf hash config
0091  * @param key_get key data
0092  * @param key_len not used
0093  * @param dbg_out output debug string
0094  * @param dbg_len debug len
0095  */
0096 exprivate void lhtest_key_debug(ndrx_lh_config_t *conf, void *key_get, 
0097         size_t key_len, char *dbg_out, size_t dbg_len)
0098 {
0099     int *p_key = (int *)key_get;
0100     snprintf(dbg_out, dbg_len, "%d", *p_key);
0101 }
0102 
0103 /**
0104  * CPM shared mem key debug value
0105  * @param conf hash config
0106  * @param idx index number
0107  * @param dbg_out debug buffer
0108  * @param dbg_len debug len
0109  */
0110 exprivate void lhtest_val_debug(ndrx_lh_config_t *conf, int idx, char *dbg_out, 
0111         size_t dbg_len)
0112 {
0113     int val = TEST_INDEX(*conf->memptr, idx)->some_val;
0114     
0115     snprintf(dbg_out, dbg_len, "%d", val);
0116 }
0117 
0118 /**
0119  * Compare the keys
0120  * @param conf hash config
0121  * @param key_get key
0122  * @param key_len key len (not used)
0123  * @param idx index at which to compare
0124  * @return 0 = equals, others not.
0125  */
0126 exprivate int lhtest_compare(ndrx_lh_config_t *conf, void *key_get, 
0127         size_t key_len, int idx)
0128 {
0129     int *key = (int *)key_get;
0130     
0131     if (TEST_INDEX((*conf->memptr), idx)->key==*key)
0132     {
0133         return 0;
0134     }
0135     else
0136     {
0137         return 1;
0138     }
0139 }
0140 
0141 /**
0142  * Load some keys with one free, the first one
0143  * @param mem memory to init
0144  * @param mark_free position to leave un-inited -> free
0145  * @param mark_was_used position to mark 
0146  */
0147 exprivate void init_one_free(ndrx_lh_test_t *mem, int mark_free)
0148 {
0149     int i;
0150     
0151     memset(mem, 0, sizeof(ndrx_lh_test_t)*TEST_MEM_MAX);
0152     
0153     for (i=0; i<TEST_MEM_MAX; i++)
0154     {
0155         if (i!=mark_free)
0156         {
0157             mem[i].some_val=i+5;
0158             mem[i].key=i;
0159             mem[i].flags=NDRX_LH_FLAG_ISUSED|NDRX_LH_FLAG_WASUSED;
0160         }
0161     }
0162 }
0163 
0164 /**
0165  * Check that every free cell we can use in the hash
0166  */
0167 Ensure(test_nstd_lh_have_space)
0168 {
0169     int i;
0170     ndrx_lh_test_t test_mem[TEST_MEM_MAX];
0171     void *p = test_mem;
0172     ndrx_lh_config_t conf;
0173     
0174     int pos;
0175     int have_value;
0176     int found;
0177     
0178     conf.elmmax = TEST_MEM_MAX;
0179     conf.elmsz = sizeof(ndrx_lh_test_t);
0180     conf.flags_offset = EXOFFSET(ndrx_lh_test_t, flags);
0181     conf.memptr = (void **)&p;
0182     conf.p_key_hash=&lhtest_key_hash;
0183     conf.p_key_debug=&lhtest_key_debug;
0184     conf.p_val_debug=&lhtest_val_debug;
0185     conf.p_compare=&lhtest_compare;
0186 
0187     for (i=TEST_MEM_MAX; i<TEST_RUN_MAX; i++)
0188     {
0189         init_one_free(test_mem, i % 25);
0190         
0191         /* lookup, there should be one position for install */
0192         found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos, 
0193                 &have_value, "lhtest");
0194         assert_equal(found, EXTRUE);
0195         
0196         /* try to install */
0197         test_mem[pos].key=i;
0198         test_mem[pos].some_val=i+6;
0199         test_mem[pos].flags=NDRX_LH_FLAG_ISUSED|NDRX_LH_FLAG_WASUSED;
0200         
0201         /* try to install again */
0202         found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos, 
0203                 &have_value, "lhtest");
0204         /* position found: */
0205         assert_equal(found, EXTRUE);
0206         
0207         /* search the value... */
0208         found=ndrx_lh_position_get(&conf, &i, sizeof(i), 0, &pos, 
0209                 &have_value, "lhtest");
0210         assert_equal(found, EXTRUE);
0211         
0212         /* verify the value */
0213         assert_equal(test_mem[pos].some_val, i+6);
0214         
0215         /* uninstall same position and try to-reuse */
0216         test_mem[pos].flags=NDRX_LH_FLAG_WASUSED;
0217         found=ndrx_lh_position_get(&conf, &i, sizeof(i), O_CREAT, &pos, 
0218                 &have_value, "lhtest");
0219         assert_equal(found, EXTRUE);
0220     }
0221 }
0222 
0223 /**
0224  * Standard library tests
0225  * @return
0226  */
0227 TestSuite *ubf_nstd_lh(void)
0228 {
0229     TestSuite *suite = create_test_suite();
0230 
0231     add_test(suite, test_nstd_lh_have_space);
0232     
0233     return suite;
0234 }
0235 /* vim: set ts=4 sw=4 et smartindent: */