mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-13 14:43:46 +01:00
56c0baa523
For everything we declare that starts with _, make it end with _ instead. This is a machine-generated patch. To make it, start by getting the list of reserved identifiers using: git ls-tree -r --name-only HEAD | grep '\.[ch]$' | \ xargs ctags --c-kinds=defglmpstuvx -o - | grep '^_' | \ cut -f 1 | sort| uniq You might need gnu ctags. Then pipe the output through this script: ============================== use strict; BEGIN { print "#!/usr/bin/perl -w -i -p\n\n"; } chomp; next if ( /^__attribute__/ or /^__func__/ or /^_FILE_OFFSET_BITS/ or /^_FORTIFY_SOURCE/ or /^_GNU_SOURCE/ or /^_WIN32/ or /^_DARWIN_UNLIMITED/ or /^_FILE_OFFSET_BITS/ or /^_LARGEFILE64_SOURCE/ or /^_LFS64_LARGEFILE/ or /^__cdecl/ or /^__attribute__/ or /^__func__/ or /^_WIN32_WINNT/); my $ident = $_; my $better = $ident; $better =~ s/^_//; $better = "${better}_"; print "s/(?<![A-Za-z0-9_])$ident(?![A-Za-z0-9_])/$better/g;\n"; ============================== Then run the resulting script on all the files you want to change. (That is, all the C except that in src/ext.) The resulting script was: ============================== s/(?<![A-Za-z0-9_])_address(?![A-Za-z0-9_])/address_/g; s/(?<![A-Za-z0-9_])_aes_fill_buf(?![A-Za-z0-9_])/aes_fill_buf_/g; s/(?<![A-Za-z0-9_])_AllowInvalid(?![A-Za-z0-9_])/AllowInvalid_/g; s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MAX(?![A-Za-z0-9_])/AP_CONN_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MIN(?![A-Za-z0-9_])/AP_CONN_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_assert_cache_ok(?![A-Za-z0-9_])/assert_cache_ok_/g; s/(?<![A-Za-z0-9_])_A_UNKNOWN(?![A-Za-z0-9_])/A_UNKNOWN_/g; s/(?<![A-Za-z0-9_])_base(?![A-Za-z0-9_])/base_/g; s/(?<![A-Za-z0-9_])_BridgePassword_AuthDigest(?![A-Za-z0-9_])/BridgePassword_AuthDigest_/g; s/(?<![A-Za-z0-9_])_buffer_stats_compare_entries(?![A-Za-z0-9_])/buffer_stats_compare_entries_/g; s/(?<![A-Za-z0-9_])_chan_circid_entries_eq(?![A-Za-z0-9_])/chan_circid_entries_eq_/g; s/(?<![A-Za-z0-9_])_chan_circid_entry_hash(?![A-Za-z0-9_])/chan_circid_entry_hash_/g; s/(?<![A-Za-z0-9_])_check_no_tls_errors(?![A-Za-z0-9_])/check_no_tls_errors_/g; s/(?<![A-Za-z0-9_])_c_hist_compare(?![A-Za-z0-9_])/c_hist_compare_/g; s/(?<![A-Za-z0-9_])_circ(?![A-Za-z0-9_])/circ_/g; s/(?<![A-Za-z0-9_])_circuit_get_global_list(?![A-Za-z0-9_])/circuit_get_global_list_/g; s/(?<![A-Za-z0-9_])_circuit_mark_for_close(?![A-Za-z0-9_])/circuit_mark_for_close_/g; s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_C_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_C_MAX_/g; s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MAX_/g; s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MIN_/g; s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MAX_/g; s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MIN_/g; s/(?<![A-Za-z0-9_])_cmp_int_strings(?![A-Za-z0-9_])/cmp_int_strings_/g; s/(?<![A-Za-z0-9_])_compare_cached_resolves_by_expiry(?![A-Za-z0-9_])/compare_cached_resolves_by_expiry_/g; s/(?<![A-Za-z0-9_])_compare_digests(?![A-Za-z0-9_])/compare_digests_/g; s/(?<![A-Za-z0-9_])_compare_digests256(?![A-Za-z0-9_])/compare_digests256_/g; s/(?<![A-Za-z0-9_])_compare_dir_src_ents_by_authority_id(?![A-Za-z0-9_])/compare_dir_src_ents_by_authority_id_/g; s/(?<![A-Za-z0-9_])_compare_duration_idx(?![A-Za-z0-9_])/compare_duration_idx_/g; s/(?<![A-Za-z0-9_])_compare_int(?![A-Za-z0-9_])/compare_int_/g; s/(?<![A-Za-z0-9_])_compare_networkstatus_v2_published_on(?![A-Za-z0-9_])/compare_networkstatus_v2_published_on_/g; s/(?<![A-Za-z0-9_])_compare_old_routers_by_identity(?![A-Za-z0-9_])/compare_old_routers_by_identity_/g; s/(?<![A-Za-z0-9_])_compare_orports(?![A-Za-z0-9_])/compare_orports_/g; s/(?<![A-Za-z0-9_])_compare_pairs(?![A-Za-z0-9_])/compare_pairs_/g; s/(?<![A-Za-z0-9_])_compare_routerinfo_by_id_digest(?![A-Za-z0-9_])/compare_routerinfo_by_id_digest_/g; s/(?<![A-Za-z0-9_])_compare_routerinfo_by_ip_and_bw(?![A-Za-z0-9_])/compare_routerinfo_by_ip_and_bw_/g; s/(?<![A-Za-z0-9_])_compare_signed_descriptors_by_age(?![A-Za-z0-9_])/compare_signed_descriptors_by_age_/g; s/(?<![A-Za-z0-9_])_compare_string_ptrs(?![A-Za-z0-9_])/compare_string_ptrs_/g; s/(?<![A-Za-z0-9_])_compare_strings_for_pqueue(?![A-Za-z0-9_])/compare_strings_for_pqueue_/g; s/(?<![A-Za-z0-9_])_compare_strs(?![A-Za-z0-9_])/compare_strs_/g; s/(?<![A-Za-z0-9_])_compare_tor_version_str_ptr(?![A-Za-z0-9_])/compare_tor_version_str_ptr_/g; s/(?<![A-Za-z0-9_])_compare_vote_rs(?![A-Za-z0-9_])/compare_vote_rs_/g; s/(?<![A-Za-z0-9_])_compare_votes_by_authority_id(?![A-Za-z0-9_])/compare_votes_by_authority_id_/g; s/(?<![A-Za-z0-9_])_compare_without_first_ch(?![A-Za-z0-9_])/compare_without_first_ch_/g; s/(?<![A-Za-z0-9_])_connection_free(?![A-Za-z0-9_])/connection_free_/g; s/(?<![A-Za-z0-9_])_connection_mark_and_flush(?![A-Za-z0-9_])/connection_mark_and_flush_/g; s/(?<![A-Za-z0-9_])_connection_mark_for_close(?![A-Za-z0-9_])/connection_mark_for_close_/g; s/(?<![A-Za-z0-9_])_connection_mark_unattached_ap(?![A-Za-z0-9_])/connection_mark_unattached_ap_/g; s/(?<![A-Za-z0-9_])_connection_write_to_buf_impl(?![A-Za-z0-9_])/connection_write_to_buf_impl_/g; s/(?<![A-Za-z0-9_])_ConnLimit(?![A-Za-z0-9_])/ConnLimit_/g; s/(?<![A-Za-z0-9_])_CONN_TYPE_MAX(?![A-Za-z0-9_])/CONN_TYPE_MAX_/g; s/(?<![A-Za-z0-9_])_CONN_TYPE_MIN(?![A-Za-z0-9_])/CONN_TYPE_MIN_/g; s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MAX(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MIN(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MAX(?![A-Za-z0-9_])/CPUWORKER_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MIN(?![A-Za-z0-9_])/CPUWORKER_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_crypto_dh_get_dh(?![A-Za-z0-9_])/crypto_dh_get_dh_/g; s/(?<![A-Za-z0-9_])_crypto_global_initialized(?![A-Za-z0-9_])/crypto_global_initialized_/g; s/(?<![A-Za-z0-9_])_crypto_new_pk_from_rsa(?![A-Za-z0-9_])/crypto_new_pk_from_rsa_/g; s/(?<![A-Za-z0-9_])_crypto_pk_get_evp_pkey(?![A-Za-z0-9_])/crypto_pk_get_evp_pkey_/g; s/(?<![A-Za-z0-9_])_crypto_pk_get_rsa(?![A-Za-z0-9_])/crypto_pk_get_rsa_/g; s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MAX(?![A-Za-z0-9_])/DIR_CONN_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MIN(?![A-Za-z0-9_])/DIR_CONN_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MAX(?![A-Za-z0-9_])/DIR_PURPOSE_MAX_/g; s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MIN(?![A-Za-z0-9_])/DIR_PURPOSE_MIN_/g; s/(?<![A-Za-z0-9_])_dirreq_map_get(?![A-Za-z0-9_])/dirreq_map_get_/g; s/(?<![A-Za-z0-9_])_dirreq_map_put(?![A-Za-z0-9_])/dirreq_map_put_/g; s/(?<![A-Za-z0-9_])_dns_randfn(?![A-Za-z0-9_])/dns_randfn_/g; s/(?<![A-Za-z0-9_])_dummy(?![A-Za-z0-9_])/dummy_/g; s/(?<![A-Za-z0-9_])_edge(?![A-Za-z0-9_])/edge_/g; s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MAX(?![A-Za-z0-9_])/END_CIRC_REASON_MAX_/g; s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MIN(?![A-Za-z0-9_])/END_CIRC_REASON_MIN_/g; s/(?<![A-Za-z0-9_])_EOF(?![A-Za-z0-9_])/EOF_/g; s/(?<![A-Za-z0-9_])_ERR(?![A-Za-z0-9_])/ERR_/g; s/(?<![A-Za-z0-9_])_escaped_val(?![A-Za-z0-9_])/escaped_val_/g; s/(?<![A-Za-z0-9_])_evdns_log(?![A-Za-z0-9_])/evdns_log_/g; s/(?<![A-Za-z0-9_])_evdns_nameserver_add_impl(?![A-Za-z0-9_])/evdns_nameserver_add_impl_/g; s/(?<![A-Za-z0-9_])_EVENT_MAX(?![A-Za-z0-9_])/EVENT_MAX_/g; s/(?<![A-Za-z0-9_])_EVENT_MIN(?![A-Za-z0-9_])/EVENT_MIN_/g; s/(?<![A-Za-z0-9_])_ExcludeExitNodesUnion(?![A-Za-z0-9_])/ExcludeExitNodesUnion_/g; s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MAX(?![A-Za-z0-9_])/EXIT_CONN_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MIN(?![A-Za-z0-9_])/EXIT_CONN_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MAX(?![A-Za-z0-9_])/EXIT_PURPOSE_MAX_/g; s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MIN(?![A-Za-z0-9_])/EXIT_PURPOSE_MIN_/g; s/(?<![A-Za-z0-9_])_extrainfo_free(?![A-Za-z0-9_])/extrainfo_free_/g; s/(?<![A-Za-z0-9_])_find_by_keyword(?![A-Za-z0-9_])/find_by_keyword_/g; s/(?<![A-Za-z0-9_])_free_cached_dir(?![A-Za-z0-9_])/free_cached_dir_/g; s/(?<![A-Za-z0-9_])_free_cached_resolve(?![A-Za-z0-9_])/free_cached_resolve_/g; s/(?<![A-Za-z0-9_])_free_duplicate_routerstatus_entry(?![A-Za-z0-9_])/free_duplicate_routerstatus_entry_/g; s/(?<![A-Za-z0-9_])_free_link_history(?![A-Za-z0-9_])/free_link_history_/g; s/(?<![A-Za-z0-9_])_geoip_compare_entries(?![A-Za-z0-9_])/geoip_compare_entries_/g; s/(?<![A-Za-z0-9_])_geoip_compare_key_to_entry(?![A-Za-z0-9_])/geoip_compare_key_to_entry_/g; s/(?<![A-Za-z0-9_])_hex_decode_digit(?![A-Za-z0-9_])/hex_decode_digit_/g; s/(?<![A-Za-z0-9_])_idxplus1(?![A-Za-z0-9_])/idxplus1_/g; s/(?<![A-Za-z0-9_])__libc_enable_secure(?![A-Za-z0-9_])/_libc_enable_secure_/g; s/(?<![A-Za-z0-9_])_log_debug(?![A-Za-z0-9_])/log_debug_/g; s/(?<![A-Za-z0-9_])_log_err(?![A-Za-z0-9_])/log_err_/g; s/(?<![A-Za-z0-9_])_log_fn(?![A-Za-z0-9_])/log_fn_/g; s/(?<![A-Za-z0-9_])_log_fn_function_name(?![A-Za-z0-9_])/log_fn_function_name_/g; s/(?<![A-Za-z0-9_])_log_global_min_severity(?![A-Za-z0-9_])/log_global_min_severity_/g; s/(?<![A-Za-z0-9_])_log_info(?![A-Za-z0-9_])/log_info_/g; s/(?<![A-Za-z0-9_])_log_notice(?![A-Za-z0-9_])/log_notice_/g; s/(?<![A-Za-z0-9_])_log_prefix(?![A-Za-z0-9_])/log_prefix_/g; s/(?<![A-Za-z0-9_])_log_warn(?![A-Za-z0-9_])/log_warn_/g; s/(?<![A-Za-z0-9_])_magic(?![A-Za-z0-9_])/magic_/g; s/(?<![A-Za-z0-9_])_MALLOC_LOCK(?![A-Za-z0-9_])/MALLOC_LOCK_/g; s/(?<![A-Za-z0-9_])_MALLOC_LOCK_INIT(?![A-Za-z0-9_])/MALLOC_LOCK_INIT_/g; s/(?<![A-Za-z0-9_])_MALLOC_UNLOCK(?![A-Za-z0-9_])/MALLOC_UNLOCK_/g; s/(?<![A-Za-z0-9_])_microdesc_eq(?![A-Za-z0-9_])/microdesc_eq_/g; s/(?<![A-Za-z0-9_])_microdesc_hash(?![A-Za-z0-9_])/microdesc_hash_/g; s/(?<![A-Za-z0-9_])_MIN_TOR_TLS_ERROR_VAL(?![A-Za-z0-9_])/MIN_TOR_TLS_ERROR_VAL_/g; s/(?<![A-Za-z0-9_])_mm_free(?![A-Za-z0-9_])/mm_free_/g; s/(?<![A-Za-z0-9_])_NIL(?![A-Za-z0-9_])/NIL_/g; s/(?<![A-Za-z0-9_])_n_openssl_mutexes(?![A-Za-z0-9_])/n_openssl_mutexes_/g; s/(?<![A-Za-z0-9_])_openssl_dynlock_create_cb(?![A-Za-z0-9_])/openssl_dynlock_create_cb_/g; s/(?<![A-Za-z0-9_])_openssl_dynlock_destroy_cb(?![A-Za-z0-9_])/openssl_dynlock_destroy_cb_/g; s/(?<![A-Za-z0-9_])_openssl_dynlock_lock_cb(?![A-Za-z0-9_])/openssl_dynlock_lock_cb_/g; s/(?<![A-Za-z0-9_])_openssl_locking_cb(?![A-Za-z0-9_])/openssl_locking_cb_/g; s/(?<![A-Za-z0-9_])_openssl_mutexes(?![A-Za-z0-9_])/openssl_mutexes_/g; s/(?<![A-Za-z0-9_])_option_abbrevs(?![A-Za-z0-9_])/option_abbrevs_/g; s/(?<![A-Za-z0-9_])_option_vars(?![A-Za-z0-9_])/option_vars_/g; s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MAX(?![A-Za-z0-9_])/OR_CONN_STATE_MAX_/g; s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MIN(?![A-Za-z0-9_])/OR_CONN_STATE_MIN_/g; s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv4(?![A-Za-z0-9_])/OutboundBindAddressIPv4_/g; s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv6(?![A-Za-z0-9_])/OutboundBindAddressIPv6_/g; s/(?<![A-Za-z0-9_])_PDS_PREFER_TUNNELED_DIR_CONNS(?![A-Za-z0-9_])/PDS_PREFER_TUNNELED_DIR_CONNS_/g; s/(?<![A-Za-z0-9_])_port(?![A-Za-z0-9_])/port_/g; s/(?<![A-Za-z0-9_])__progname(?![A-Za-z0-9_])/_progname_/g; s/(?<![A-Za-z0-9_])_PublishServerDescriptor(?![A-Za-z0-9_])/PublishServerDescriptor_/g; s/(?<![A-Za-z0-9_])_remove_old_client_helper(?![A-Za-z0-9_])/remove_old_client_helper_/g; s/(?<![A-Za-z0-9_])_rend_cache_entry_free(?![A-Za-z0-9_])/rend_cache_entry_free_/g; s/(?<![A-Za-z0-9_])_routerlist_find_elt(?![A-Za-z0-9_])/routerlist_find_elt_/g; s/(?<![A-Za-z0-9_])_SafeLogging(?![A-Za-z0-9_])/SafeLogging_/g; s/(?<![A-Za-z0-9_])_SHORT_FILE_(?![A-Za-z0-9_])/SHORT_FILE__/g; s/(?<![A-Za-z0-9_])_state_abbrevs(?![A-Za-z0-9_])/state_abbrevs_/g; s/(?<![A-Za-z0-9_])_state_vars(?![A-Za-z0-9_])/state_vars_/g; s/(?<![A-Za-z0-9_])_t(?![A-Za-z0-9_])/t_/g; s/(?<![A-Za-z0-9_])_t32(?![A-Za-z0-9_])/t32_/g; s/(?<![A-Za-z0-9_])_test_op_ip6(?![A-Za-z0-9_])/test_op_ip6_/g; s/(?<![A-Za-z0-9_])_thread1_name(?![A-Za-z0-9_])/thread1_name_/g; s/(?<![A-Za-z0-9_])_thread2_name(?![A-Za-z0-9_])/thread2_name_/g; s/(?<![A-Za-z0-9_])_thread_test_func(?![A-Za-z0-9_])/thread_test_func_/g; s/(?<![A-Za-z0-9_])_thread_test_mutex(?![A-Za-z0-9_])/thread_test_mutex_/g; s/(?<![A-Za-z0-9_])_thread_test_start1(?![A-Za-z0-9_])/thread_test_start1_/g; s/(?<![A-Za-z0-9_])_thread_test_start2(?![A-Za-z0-9_])/thread_test_start2_/g; s/(?<![A-Za-z0-9_])_thread_test_strmap(?![A-Za-z0-9_])/thread_test_strmap_/g; s/(?<![A-Za-z0-9_])_tor_calloc(?![A-Za-z0-9_])/tor_calloc_/g; s/(?<![A-Za-z0-9_])_TOR_CHANNEL_INTERNAL(?![A-Za-z0-9_])/TOR_CHANNEL_INTERNAL_/g; s/(?<![A-Za-z0-9_])_TOR_CIRCUITMUX_EWMA_C(?![A-Za-z0-9_])/TOR_CIRCUITMUX_EWMA_C_/g; s/(?<![A-Za-z0-9_])_tor_free(?![A-Za-z0-9_])/tor_free_/g; s/(?<![A-Za-z0-9_])_tor_malloc(?![A-Za-z0-9_])/tor_malloc_/g; s/(?<![A-Za-z0-9_])_tor_malloc_zero(?![A-Za-z0-9_])/tor_malloc_zero_/g; s/(?<![A-Za-z0-9_])_tor_memdup(?![A-Za-z0-9_])/tor_memdup_/g; s/(?<![A-Za-z0-9_])_tor_realloc(?![A-Za-z0-9_])/tor_realloc_/g; s/(?<![A-Za-z0-9_])_tor_strdup(?![A-Za-z0-9_])/tor_strdup_/g; s/(?<![A-Za-z0-9_])_tor_strndup(?![A-Za-z0-9_])/tor_strndup_/g; s/(?<![A-Za-z0-9_])_TOR_TLS_SYSCALL(?![A-Za-z0-9_])/TOR_TLS_SYSCALL_/g; s/(?<![A-Za-z0-9_])_TOR_TLS_ZERORETURN(?![A-Za-z0-9_])/TOR_TLS_ZERORETURN_/g; s/(?<![A-Za-z0-9_])__USE_ISOC99(?![A-Za-z0-9_])/_USE_ISOC99_/g; s/(?<![A-Za-z0-9_])_UsingTestNetworkDefaults(?![A-Za-z0-9_])/UsingTestNetworkDefaults_/g; s/(?<![A-Za-z0-9_])_val(?![A-Za-z0-9_])/val_/g; s/(?<![A-Za-z0-9_])_void_for_alignment(?![A-Za-z0-9_])/void_for_alignment_/g; ==============================
766 lines
24 KiB
C
766 lines
24 KiB
C
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
|
/* See LICENSE for licensing information */
|
|
|
|
#include "orconfig.h"
|
|
#include "or.h"
|
|
#include "test.h"
|
|
|
|
/** Helper: return a tristate based on comparing the strings in *<b>a</b> and
|
|
* *<b>b</b>. */
|
|
static int
|
|
compare_strs_(const void **a, const void **b)
|
|
{
|
|
const char *s1 = *a, *s2 = *b;
|
|
return strcmp(s1, s2);
|
|
}
|
|
|
|
/** Helper: return a tristate based on comparing the strings in *<b>a</b> and
|
|
* *<b>b</b>, excluding a's first character, and ignoring case. */
|
|
static int
|
|
compare_without_first_ch_(const void *a, const void **b)
|
|
{
|
|
const char *s1 = a, *s2 = *b;
|
|
return strcasecmp(s1+1, s2);
|
|
}
|
|
|
|
/** Run unit tests for basic dynamic-sized array functionality. */
|
|
static void
|
|
test_container_smartlist_basic(void)
|
|
{
|
|
smartlist_t *sl;
|
|
|
|
/* XXXX test sort_digests, uniq_strings, uniq_digests */
|
|
|
|
/* Test smartlist add, del_keeporder, insert, get. */
|
|
sl = smartlist_new();
|
|
smartlist_add(sl, (void*)1);
|
|
smartlist_add(sl, (void*)2);
|
|
smartlist_add(sl, (void*)3);
|
|
smartlist_add(sl, (void*)4);
|
|
smartlist_del_keeporder(sl, 1);
|
|
smartlist_insert(sl, 1, (void*)22);
|
|
smartlist_insert(sl, 0, (void*)0);
|
|
smartlist_insert(sl, 5, (void*)555);
|
|
test_eq_ptr((void*)0, smartlist_get(sl,0));
|
|
test_eq_ptr((void*)1, smartlist_get(sl,1));
|
|
test_eq_ptr((void*)22, smartlist_get(sl,2));
|
|
test_eq_ptr((void*)3, smartlist_get(sl,3));
|
|
test_eq_ptr((void*)4, smartlist_get(sl,4));
|
|
test_eq_ptr((void*)555, smartlist_get(sl,5));
|
|
/* Try deleting in the middle. */
|
|
smartlist_del(sl, 1);
|
|
test_eq_ptr((void*)555, smartlist_get(sl, 1));
|
|
/* Try deleting at the end. */
|
|
smartlist_del(sl, 4);
|
|
test_eq(4, smartlist_len(sl));
|
|
|
|
/* test isin. */
|
|
test_assert(smartlist_isin(sl, (void*)3));
|
|
test_assert(!smartlist_isin(sl, (void*)99));
|
|
|
|
done:
|
|
smartlist_free(sl);
|
|
}
|
|
|
|
/** Run unit tests for smartlist-of-strings functionality. */
|
|
static void
|
|
test_container_smartlist_strings(void)
|
|
{
|
|
smartlist_t *sl = smartlist_new();
|
|
char *cp=NULL, *cp_alloc=NULL;
|
|
size_t sz;
|
|
|
|
/* Test split and join */
|
|
test_eq(0, smartlist_len(sl));
|
|
smartlist_split_string(sl, "abc", ":", 0, 0);
|
|
test_eq(1, smartlist_len(sl));
|
|
test_streq("abc", smartlist_get(sl, 0));
|
|
smartlist_split_string(sl, "a::bc::", "::", 0, 0);
|
|
test_eq(4, smartlist_len(sl));
|
|
test_streq("a", smartlist_get(sl, 1));
|
|
test_streq("bc", smartlist_get(sl, 2));
|
|
test_streq("", smartlist_get(sl, 3));
|
|
cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
|
|
test_streq(cp_alloc, "abcabc");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
|
|
test_streq(cp_alloc, "abc!a!bc!");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
|
|
test_streq(cp_alloc, "abcXYaXYbcXY");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
|
|
test_streq(cp_alloc, "abcXYaXYbcXYXY");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
|
|
test_streq(cp_alloc, "abcabc");
|
|
tor_free(cp_alloc);
|
|
|
|
smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
|
|
test_eq(8, smartlist_len(sl));
|
|
test_streq("", smartlist_get(sl, 4));
|
|
test_streq("def", smartlist_get(sl, 5));
|
|
test_streq(" ", smartlist_get(sl, 6));
|
|
test_streq("ghijk", smartlist_get(sl, 7));
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
|
|
test_eq(3, smartlist_len(sl));
|
|
test_streq("a", smartlist_get(sl,0));
|
|
test_streq("bbd", smartlist_get(sl,1));
|
|
test_streq("cdef", smartlist_get(sl,2));
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
SPLIT_SKIP_SPACE, 0);
|
|
test_eq(8, smartlist_len(sl));
|
|
test_streq("z", smartlist_get(sl,3));
|
|
test_streq("zhasd", smartlist_get(sl,4));
|
|
test_streq("", smartlist_get(sl,5));
|
|
test_streq("bnud", smartlist_get(sl,6));
|
|
test_streq("", smartlist_get(sl,7));
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
smartlist_split_string(sl, " ab\tc \td ef ", NULL,
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
test_eq(4, smartlist_len(sl));
|
|
test_streq("ab", smartlist_get(sl,0));
|
|
test_streq("c", smartlist_get(sl,1));
|
|
test_streq("d", smartlist_get(sl,2));
|
|
test_streq("ef", smartlist_get(sl,3));
|
|
smartlist_split_string(sl, "ghi\tj", NULL,
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
test_eq(6, smartlist_len(sl));
|
|
test_streq("ghi", smartlist_get(sl,4));
|
|
test_streq("j", smartlist_get(sl,5));
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
|
|
test_streq(cp_alloc, "");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
|
|
test_streq(cp_alloc, "XY");
|
|
tor_free(cp_alloc);
|
|
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
test_eq(3, smartlist_len(sl));
|
|
test_streq("z", smartlist_get(sl, 0));
|
|
test_streq("zhasd", smartlist_get(sl, 1));
|
|
test_streq("bnud", smartlist_get(sl, 2));
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
|
|
test_eq(5, smartlist_len(sl));
|
|
test_streq("z", smartlist_get(sl, 3));
|
|
test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
smartlist_split_string(sl, "abcd\n", "\n",
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
test_eq(1, smartlist_len(sl));
|
|
test_streq("abcd", smartlist_get(sl, 0));
|
|
smartlist_split_string(sl, "efgh", "\n",
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
test_eq(2, smartlist_len(sl));
|
|
test_streq("efgh", smartlist_get(sl, 1));
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
/* Test swapping, shuffling, and sorting. */
|
|
smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
|
|
test_eq(7, smartlist_len(sl));
|
|
smartlist_sort(sl, compare_strs_);
|
|
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
|
|
test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
|
|
tor_free(cp_alloc);
|
|
smartlist_swap(sl, 1, 5);
|
|
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
|
|
test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
|
|
tor_free(cp_alloc);
|
|
smartlist_shuffle(sl);
|
|
test_eq(7, smartlist_len(sl));
|
|
test_assert(smartlist_string_isin(sl, "and"));
|
|
test_assert(smartlist_string_isin(sl, "router"));
|
|
test_assert(smartlist_string_isin(sl, "by"));
|
|
test_assert(smartlist_string_isin(sl, "nickm"));
|
|
test_assert(smartlist_string_isin(sl, "onion"));
|
|
test_assert(smartlist_string_isin(sl, "arma"));
|
|
test_assert(smartlist_string_isin(sl, "the"));
|
|
|
|
/* Test bsearch. */
|
|
smartlist_sort(sl, compare_strs_);
|
|
test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
|
|
compare_without_first_ch_));
|
|
test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
|
|
test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));
|
|
|
|
/* Test bsearch_idx */
|
|
{
|
|
int f;
|
|
test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
|
|
test_eq(f, 0);
|
|
test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
|
|
test_eq(f, 1);
|
|
test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
|
|
test_eq(f, 0);
|
|
test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
|
|
test_eq(f, 1);
|
|
test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
|
|
test_eq(f, 0);
|
|
test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
|
|
test_eq(f, 0);
|
|
}
|
|
|
|
/* Test reverse() and pop_last() */
|
|
smartlist_reverse(sl);
|
|
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
|
|
test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
|
|
tor_free(cp_alloc);
|
|
cp_alloc = smartlist_pop_last(sl);
|
|
test_streq(cp_alloc, "and");
|
|
tor_free(cp_alloc);
|
|
test_eq(smartlist_len(sl), 6);
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
cp_alloc = smartlist_pop_last(sl);
|
|
test_eq(cp_alloc, NULL);
|
|
|
|
/* Test uniq() */
|
|
smartlist_split_string(sl,
|
|
"50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
|
|
",", 0, 0);
|
|
smartlist_sort(sl, compare_strs_);
|
|
smartlist_uniq(sl, compare_strs_, tor_free_);
|
|
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
|
|
test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
|
|
tor_free(cp_alloc);
|
|
|
|
/* Test string_isin and isin_case and num_isin */
|
|
test_assert(smartlist_string_isin(sl, "noon"));
|
|
test_assert(!smartlist_string_isin(sl, "noonoon"));
|
|
test_assert(smartlist_string_isin_case(sl, "nOOn"));
|
|
test_assert(!smartlist_string_isin_case(sl, "nooNooN"));
|
|
test_assert(smartlist_string_num_isin(sl, 50));
|
|
test_assert(!smartlist_string_num_isin(sl, 60));
|
|
|
|
/* Test smartlist_choose */
|
|
{
|
|
int i;
|
|
int allsame = 1;
|
|
int allin = 1;
|
|
void *first = smartlist_choose(sl);
|
|
test_assert(smartlist_isin(sl, first));
|
|
for (i = 0; i < 100; ++i) {
|
|
void *second = smartlist_choose(sl);
|
|
if (second != first)
|
|
allsame = 0;
|
|
if (!smartlist_isin(sl, second))
|
|
allin = 0;
|
|
}
|
|
test_assert(!allsame);
|
|
test_assert(allin);
|
|
}
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_clear(sl);
|
|
|
|
/* Test string_remove and remove and join_strings2 */
|
|
smartlist_split_string(sl,
|
|
"Some say the Earth will end in ice and some in fire",
|
|
" ", 0, 0);
|
|
cp = smartlist_get(sl, 4);
|
|
test_streq(cp, "will");
|
|
smartlist_add(sl, cp);
|
|
smartlist_remove(sl, cp);
|
|
tor_free(cp);
|
|
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
|
|
test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
|
|
tor_free(cp_alloc);
|
|
smartlist_string_remove(sl, "in");
|
|
cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
|
|
test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
|
|
test_eq((int)sz, 40);
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_free(sl);
|
|
tor_free(cp_alloc);
|
|
}
|
|
|
|
/** Run unit tests for smartlist set manipulation functions. */
|
|
static void
|
|
test_container_smartlist_overlap(void)
|
|
{
|
|
smartlist_t *sl = smartlist_new();
|
|
smartlist_t *ints = smartlist_new();
|
|
smartlist_t *odds = smartlist_new();
|
|
smartlist_t *evens = smartlist_new();
|
|
smartlist_t *primes = smartlist_new();
|
|
int i;
|
|
for (i=1; i < 10; i += 2)
|
|
smartlist_add(odds, (void*)(uintptr_t)i);
|
|
for (i=0; i < 10; i += 2)
|
|
smartlist_add(evens, (void*)(uintptr_t)i);
|
|
|
|
/* add_all */
|
|
smartlist_add_all(ints, odds);
|
|
smartlist_add_all(ints, evens);
|
|
test_eq(smartlist_len(ints), 10);
|
|
|
|
smartlist_add(primes, (void*)2);
|
|
smartlist_add(primes, (void*)3);
|
|
smartlist_add(primes, (void*)5);
|
|
smartlist_add(primes, (void*)7);
|
|
|
|
/* overlap */
|
|
test_assert(smartlist_overlap(ints, odds));
|
|
test_assert(smartlist_overlap(odds, primes));
|
|
test_assert(smartlist_overlap(evens, primes));
|
|
test_assert(!smartlist_overlap(odds, evens));
|
|
|
|
/* intersect */
|
|
smartlist_add_all(sl, odds);
|
|
smartlist_intersect(sl, primes);
|
|
test_eq(smartlist_len(sl), 3);
|
|
test_assert(smartlist_isin(sl, (void*)3));
|
|
test_assert(smartlist_isin(sl, (void*)5));
|
|
test_assert(smartlist_isin(sl, (void*)7));
|
|
|
|
/* subtract */
|
|
smartlist_add_all(sl, primes);
|
|
smartlist_subtract(sl, odds);
|
|
test_eq(smartlist_len(sl), 1);
|
|
test_assert(smartlist_isin(sl, (void*)2));
|
|
|
|
done:
|
|
smartlist_free(odds);
|
|
smartlist_free(evens);
|
|
smartlist_free(ints);
|
|
smartlist_free(primes);
|
|
smartlist_free(sl);
|
|
}
|
|
|
|
/** Run unit tests for smartlist-of-digests functions. */
|
|
static void
|
|
test_container_smartlist_digests(void)
|
|
{
|
|
smartlist_t *sl = smartlist_new();
|
|
|
|
/* digest_isin. */
|
|
smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
|
|
smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
|
|
smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
|
|
test_eq(0, smartlist_digest_isin(NULL, "AAAAAAAAAAAAAAAAAAAA"));
|
|
test_assert(smartlist_digest_isin(sl, "AAAAAAAAAAAAAAAAAAAA"));
|
|
test_assert(smartlist_digest_isin(sl, "\00090AAB2AAAAaasdAAAAA"));
|
|
test_eq(0, smartlist_digest_isin(sl, "\00090AAB2AAABaasdAAAAA"));
|
|
|
|
/* sort digests */
|
|
smartlist_sort_digests(sl);
|
|
test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
|
|
test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
|
|
test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
|
|
test_eq(3, smartlist_len(sl));
|
|
|
|
/* uniq_digests */
|
|
smartlist_uniq_digests(sl);
|
|
test_eq(2, smartlist_len(sl));
|
|
test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
|
|
test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
|
|
|
|
done:
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_free(sl);
|
|
}
|
|
|
|
/** Run unit tests for concatenate-a-smartlist-of-strings functions. */
|
|
static void
|
|
test_container_smartlist_join(void)
|
|
{
|
|
smartlist_t *sl = smartlist_new();
|
|
smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
|
|
*sl4 = smartlist_new();
|
|
char *joined=NULL;
|
|
/* unique, sorted. */
|
|
smartlist_split_string(sl,
|
|
"Abashments Ambush Anchorman Bacon Banks Borscht "
|
|
"Bunks Inhumane Insurance Knish Know Manners "
|
|
"Maraschinos Stamina Sunbonnets Unicorns Wombats",
|
|
" ", 0, 0);
|
|
/* non-unique, sorted. */
|
|
smartlist_split_string(sl2,
|
|
"Ambush Anchorman Anchorman Anemias Anemias Bacon "
|
|
"Crossbowmen Inhumane Insurance Knish Know Manners "
|
|
"Manners Maraschinos Wombats Wombats Work",
|
|
" ", 0, 0);
|
|
SMARTLIST_FOREACH_JOIN(sl, char *, cp1,
|
|
sl2, char *, cp2,
|
|
strcmp(cp1,cp2),
|
|
smartlist_add(sl3, cp2)) {
|
|
test_streq(cp1, cp2);
|
|
smartlist_add(sl4, cp1);
|
|
} SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
|
|
|
|
SMARTLIST_FOREACH(sl3, const char *, cp,
|
|
test_assert(smartlist_isin(sl2, cp) &&
|
|
!smartlist_string_isin(sl, cp)));
|
|
SMARTLIST_FOREACH(sl4, const char *, cp,
|
|
test_assert(smartlist_isin(sl, cp) &&
|
|
smartlist_string_isin(sl2, cp)));
|
|
joined = smartlist_join_strings(sl3, ",", 0, NULL);
|
|
test_streq(joined, "Anemias,Anemias,Crossbowmen,Work");
|
|
tor_free(joined);
|
|
joined = smartlist_join_strings(sl4, ",", 0, NULL);
|
|
test_streq(joined, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
|
|
"Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
|
|
tor_free(joined);
|
|
|
|
done:
|
|
smartlist_free(sl4);
|
|
smartlist_free(sl3);
|
|
SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
|
|
smartlist_free(sl2);
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
smartlist_free(sl);
|
|
tor_free(joined);
|
|
}
|
|
|
|
/** Run unit tests for bitarray code */
|
|
static void
|
|
test_container_bitarray(void)
|
|
{
|
|
bitarray_t *ba = NULL;
|
|
int i, j, ok=1;
|
|
|
|
ba = bitarray_init_zero(1);
|
|
test_assert(ba);
|
|
test_assert(! bitarray_is_set(ba, 0));
|
|
bitarray_set(ba, 0);
|
|
test_assert(bitarray_is_set(ba, 0));
|
|
bitarray_clear(ba, 0);
|
|
test_assert(! bitarray_is_set(ba, 0));
|
|
bitarray_free(ba);
|
|
|
|
ba = bitarray_init_zero(1023);
|
|
for (i = 1; i < 64; ) {
|
|
for (j = 0; j < 1023; ++j) {
|
|
if (j % i)
|
|
bitarray_set(ba, j);
|
|
else
|
|
bitarray_clear(ba, j);
|
|
}
|
|
for (j = 0; j < 1023; ++j) {
|
|
if (!bool_eq(bitarray_is_set(ba, j), j%i))
|
|
ok = 0;
|
|
}
|
|
test_assert(ok);
|
|
if (i < 7)
|
|
++i;
|
|
else if (i == 28)
|
|
i = 32;
|
|
else
|
|
i += 7;
|
|
}
|
|
|
|
done:
|
|
if (ba)
|
|
bitarray_free(ba);
|
|
}
|
|
|
|
/** Run unit tests for digest set code (implemented as a hashtable or as a
|
|
* bloom filter) */
|
|
static void
|
|
test_container_digestset(void)
|
|
{
|
|
smartlist_t *included = smartlist_new();
|
|
char d[DIGEST_LEN];
|
|
int i;
|
|
int ok = 1;
|
|
int false_positives = 0;
|
|
digestset_t *set = NULL;
|
|
|
|
for (i = 0; i < 1000; ++i) {
|
|
crypto_rand(d, DIGEST_LEN);
|
|
smartlist_add(included, tor_memdup(d, DIGEST_LEN));
|
|
}
|
|
set = digestset_new(1000);
|
|
SMARTLIST_FOREACH(included, const char *, cp,
|
|
if (digestset_isin(set, cp))
|
|
ok = 0);
|
|
test_assert(ok);
|
|
SMARTLIST_FOREACH(included, const char *, cp,
|
|
digestset_add(set, cp));
|
|
SMARTLIST_FOREACH(included, const char *, cp,
|
|
if (!digestset_isin(set, cp))
|
|
ok = 0);
|
|
test_assert(ok);
|
|
for (i = 0; i < 1000; ++i) {
|
|
crypto_rand(d, DIGEST_LEN);
|
|
if (digestset_isin(set, d))
|
|
++false_positives;
|
|
}
|
|
test_assert(false_positives < 50); /* Should be far lower. */
|
|
|
|
done:
|
|
if (set)
|
|
digestset_free(set);
|
|
SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
|
|
smartlist_free(included);
|
|
}
|
|
|
|
typedef struct pq_entry_t {
|
|
const char *val;
|
|
int idx;
|
|
} pq_entry_t;
|
|
|
|
/** Helper: return a tristate based on comparing two pq_entry_t values. */
|
|
static int
|
|
compare_strings_for_pqueue_(const void *p1, const void *p2)
|
|
{
|
|
const pq_entry_t *e1=p1, *e2=p2;
|
|
return strcmp(e1->val, e2->val);
|
|
}
|
|
|
|
/** Run unit tests for heap-based priority queue functions. */
|
|
static void
|
|
test_container_pqueue(void)
|
|
{
|
|
smartlist_t *sl = smartlist_new();
|
|
int (*cmp)(const void *, const void*);
|
|
const int offset = STRUCT_OFFSET(pq_entry_t, idx);
|
|
#define ENTRY(s) pq_entry_t s = { #s, -1 }
|
|
ENTRY(cows);
|
|
ENTRY(zebras);
|
|
ENTRY(fish);
|
|
ENTRY(frogs);
|
|
ENTRY(apples);
|
|
ENTRY(squid);
|
|
ENTRY(daschunds);
|
|
ENTRY(eggplants);
|
|
ENTRY(weissbier);
|
|
ENTRY(lobsters);
|
|
ENTRY(roquefort);
|
|
ENTRY(chinchillas);
|
|
ENTRY(fireflies);
|
|
|
|
#define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
|
|
|
|
cmp = compare_strings_for_pqueue_;
|
|
smartlist_pqueue_add(sl, cmp, offset, &cows);
|
|
smartlist_pqueue_add(sl, cmp, offset, &zebras);
|
|
smartlist_pqueue_add(sl, cmp, offset, &fish);
|
|
smartlist_pqueue_add(sl, cmp, offset, &frogs);
|
|
smartlist_pqueue_add(sl, cmp, offset, &apples);
|
|
smartlist_pqueue_add(sl, cmp, offset, &squid);
|
|
smartlist_pqueue_add(sl, cmp, offset, &daschunds);
|
|
smartlist_pqueue_add(sl, cmp, offset, &eggplants);
|
|
smartlist_pqueue_add(sl, cmp, offset, &weissbier);
|
|
smartlist_pqueue_add(sl, cmp, offset, &lobsters);
|
|
smartlist_pqueue_add(sl, cmp, offset, &roquefort);
|
|
|
|
OK();
|
|
|
|
test_eq(smartlist_len(sl), 11);
|
|
test_eq_ptr(smartlist_get(sl, 0), &apples);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
|
|
test_eq(smartlist_len(sl), 10);
|
|
OK();
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
|
|
smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
|
|
OK();
|
|
smartlist_pqueue_add(sl, cmp, offset, &fireflies);
|
|
OK();
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
|
|
OK();
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
|
|
OK();
|
|
test_eq(smartlist_len(sl), 3);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
|
|
test_eq(smartlist_len(sl), 0);
|
|
OK();
|
|
|
|
/* Now test remove. */
|
|
smartlist_pqueue_add(sl, cmp, offset, &cows);
|
|
smartlist_pqueue_add(sl, cmp, offset, &fish);
|
|
smartlist_pqueue_add(sl, cmp, offset, &frogs);
|
|
smartlist_pqueue_add(sl, cmp, offset, &apples);
|
|
smartlist_pqueue_add(sl, cmp, offset, &squid);
|
|
smartlist_pqueue_add(sl, cmp, offset, &zebras);
|
|
test_eq(smartlist_len(sl), 6);
|
|
OK();
|
|
smartlist_pqueue_remove(sl, cmp, offset, &zebras);
|
|
test_eq(smartlist_len(sl), 5);
|
|
OK();
|
|
smartlist_pqueue_remove(sl, cmp, offset, &cows);
|
|
test_eq(smartlist_len(sl), 4);
|
|
OK();
|
|
smartlist_pqueue_remove(sl, cmp, offset, &apples);
|
|
test_eq(smartlist_len(sl), 3);
|
|
OK();
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
|
|
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
|
|
test_eq(smartlist_len(sl), 0);
|
|
OK();
|
|
|
|
#undef OK
|
|
|
|
done:
|
|
|
|
smartlist_free(sl);
|
|
}
|
|
|
|
/** Run unit tests for string-to-void* map functions */
|
|
static void
|
|
test_container_strmap(void)
|
|
{
|
|
strmap_t *map;
|
|
strmap_iter_t *iter;
|
|
const char *k;
|
|
void *v;
|
|
char *visited = NULL;
|
|
smartlist_t *found_keys = NULL;
|
|
|
|
map = strmap_new();
|
|
test_assert(map);
|
|
test_eq(strmap_size(map), 0);
|
|
test_assert(strmap_isempty(map));
|
|
v = strmap_set(map, "K1", (void*)99);
|
|
test_eq(v, NULL);
|
|
test_assert(!strmap_isempty(map));
|
|
v = strmap_set(map, "K2", (void*)101);
|
|
test_eq(v, NULL);
|
|
v = strmap_set(map, "K1", (void*)100);
|
|
test_eq(v, (void*)99);
|
|
test_eq_ptr(strmap_get(map,"K1"), (void*)100);
|
|
test_eq_ptr(strmap_get(map,"K2"), (void*)101);
|
|
test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
|
|
strmap_assert_ok(map);
|
|
|
|
v = strmap_remove(map,"K2");
|
|
strmap_assert_ok(map);
|
|
test_eq_ptr(v, (void*)101);
|
|
test_eq_ptr(strmap_get(map,"K2"), NULL);
|
|
test_eq_ptr(strmap_remove(map,"K2"), NULL);
|
|
|
|
strmap_set(map, "K2", (void*)101);
|
|
strmap_set(map, "K3", (void*)102);
|
|
strmap_set(map, "K4", (void*)103);
|
|
test_eq(strmap_size(map), 4);
|
|
strmap_assert_ok(map);
|
|
strmap_set(map, "K5", (void*)104);
|
|
strmap_set(map, "K6", (void*)105);
|
|
strmap_assert_ok(map);
|
|
|
|
/* Test iterator. */
|
|
iter = strmap_iter_init(map);
|
|
found_keys = smartlist_new();
|
|
while (!strmap_iter_done(iter)) {
|
|
strmap_iter_get(iter,&k,&v);
|
|
smartlist_add(found_keys, tor_strdup(k));
|
|
test_eq_ptr(v, strmap_get(map, k));
|
|
|
|
if (!strcmp(k, "K2")) {
|
|
iter = strmap_iter_next_rmv(map,iter);
|
|
} else {
|
|
iter = strmap_iter_next(map,iter);
|
|
}
|
|
}
|
|
|
|
/* Make sure we removed K2, but not the others. */
|
|
test_eq_ptr(strmap_get(map, "K2"), NULL);
|
|
test_eq_ptr(strmap_get(map, "K5"), (void*)104);
|
|
/* Make sure we visited everyone once */
|
|
smartlist_sort_strings(found_keys);
|
|
visited = smartlist_join_strings(found_keys, ":", 0, NULL);
|
|
test_streq(visited, "K1:K2:K3:K4:K5:K6");
|
|
|
|
strmap_assert_ok(map);
|
|
/* Clean up after ourselves. */
|
|
strmap_free(map, NULL);
|
|
map = NULL;
|
|
|
|
/* Now try some lc functions. */
|
|
map = strmap_new();
|
|
strmap_set_lc(map,"Ab.C", (void*)1);
|
|
test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
|
|
strmap_assert_ok(map);
|
|
test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
|
|
test_eq_ptr(strmap_get(map,"AB.C"), NULL);
|
|
test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
|
|
strmap_assert_ok(map);
|
|
test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
|
|
|
|
done:
|
|
if (map)
|
|
strmap_free(map,NULL);
|
|
if (found_keys) {
|
|
SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
|
|
smartlist_free(found_keys);
|
|
}
|
|
tor_free(visited);
|
|
}
|
|
|
|
/** Run unit tests for getting the median of a list. */
|
|
static void
|
|
test_container_order_functions(void)
|
|
{
|
|
int lst[25], n = 0;
|
|
// int a=12,b=24,c=25,d=60,e=77;
|
|
|
|
#define median() median_int(lst, n)
|
|
|
|
lst[n++] = 12;
|
|
test_eq(12, median()); /* 12 */
|
|
lst[n++] = 77;
|
|
//smartlist_shuffle(sl);
|
|
test_eq(12, median()); /* 12, 77 */
|
|
lst[n++] = 77;
|
|
//smartlist_shuffle(sl);
|
|
test_eq(77, median()); /* 12, 77, 77 */
|
|
lst[n++] = 24;
|
|
test_eq(24, median()); /* 12,24,77,77 */
|
|
lst[n++] = 60;
|
|
lst[n++] = 12;
|
|
lst[n++] = 25;
|
|
//smartlist_shuffle(sl);
|
|
test_eq(25, median()); /* 12,12,24,25,60,77,77 */
|
|
#undef median
|
|
|
|
done:
|
|
;
|
|
}
|
|
|
|
#define CONTAINER_LEGACY(name) \
|
|
{ #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
|
|
|
|
struct testcase_t container_tests[] = {
|
|
CONTAINER_LEGACY(smartlist_basic),
|
|
CONTAINER_LEGACY(smartlist_strings),
|
|
CONTAINER_LEGACY(smartlist_overlap),
|
|
CONTAINER_LEGACY(smartlist_digests),
|
|
CONTAINER_LEGACY(smartlist_join),
|
|
CONTAINER_LEGACY(bitarray),
|
|
CONTAINER_LEGACY(digestset),
|
|
CONTAINER_LEGACY(strmap),
|
|
CONTAINER_LEGACY(pqueue),
|
|
CONTAINER_LEGACY(order_functions),
|
|
END_OF_TESTCASES
|
|
};
|
|
|