Mercurial > hg > audiodb
view tests/test-utils.sh @ 408:f0a69693eaef api-inversion
The lesser of two evils, part 1.
Most of the body of audiodb_insert_datum() will apply to
"LARGE_ADB"-type insertions: checking for the right flags, checking for
enough space free, synchronizing the header. Wouldn't it be nice if we
could reuse all that code (or at least the bits that apply) without one
horrible almost-identical cut-and-paste job (see
batchinsert_large_adb(), or if that's not compelling enough, the four
almost-identical query loops from before the Great Refactoring).
Well, yes, it would. Sadly C makes it mildly difficult, because its
functions are explicitly typed (so we can't pass arbitrary arguments of
other types, even if they're ABI-compatible), while its macros are
textual (which makes writing and maintaining them horrible). The
thought of a union argument was briefly entertained and then discarded
as being just Too Weird.
So, instead, (ab)use the oldest trick in the book: void *. Define an
adb_datum_internal_t which has void * instead of double *; the intention
is that this internal data type can be constructed both from an
adb_datum_t and some notional adb_reference_t (which looks very much
like an adb_insert_t at the time of writing, with char * structure
entries representing filenames). This adb_datum_internal_t structure is
very much an internals-only thing, so put its definition in the
internals header.
Call what was previously audiodb_insert_datum() a new function
audiodb_insert_datum_internal(), made static so that really no-one is
tempted to call it other than ourselves. audiodb_insert_datum() is then
trivial in terms of this new function, if stupidly tedious. (If we were
playing dangerously, we could just perform a cast, but relying on the
fact that sizeof(double *) = sizeof(void *) would almost certainly end
up biting when we least expect.
Incidental inclusion in this patch, since I noticed it at the time:
actually check for the O2_FLAG_L2NORM before scribbling all over the
l2norm table. Somewhat unsurprisingly, there are as yet no tests to
defend against this (harmless, as it turns out) erroneous behaviour.
author | mas01cr |
---|---|
date | Tue, 09 Dec 2008 20:53:39 +0000 |
parents | e21cc48ddf4d |
children | d5ada9532a40 |
line wrap: on
line source
# no shebang line: this file should be sourced by run-test.sh files set -E trap "exit 1" ERR if [ -z "${AUDIODB}" ]; then AUDIODB=../../audioDB fi # FIXME: maybe generalize to multiple arguments? Also, implement it # properly, rather than just for a few floats that we know how to # encode. This might involve writing some C code, as Bash doesn't do # Floating Point. (scanf() is probably enough). expect_clean_error_exit() { trap - ERR "$@" exit_code=$? trap "exit 1" ERR if [ $exit_code -eq 0 ]; then exit 1 elif [ $exit_code -ge 126 ]; then exit 1 fi } floatstring() { for arg in "$@"; do case ${arg} in 0) printf "\x00\x00\x00\x00\x00\x00\x00\x00";; -0.5) printf "\x00\x00\x00\x00\x00\x00\xe0\xbf";; 0.5) printf "\x00\x00\x00\x00\x00\x00\xe0\x3f";; -1) printf "\x00\x00\x00\x00\x00\x00\xf0\xbf";; 1) printf "\x00\x00\x00\x00\x00\x00\xf0\x3f";; *) echo "bad arg to floatstring(): ${arg}" exit 1;; esac done } # FIXME: likewise. And endianness issues (which are a reflection of # the endianness of audioDB as of 2007-09-18, unfortunately). intstring() { # works up to 9 for now if [ $1 -ge 10 ]; then echo "intstring() arg too large: ${1}"; exit 1; fi printf "%b\x00\x00\x00" "\\x${1}" } # Web services utilities start_server() { $1 -s $2 & # HACK: deal with race on process creation sleep 1 trap 'kill $!; exit 1' ERR } stop_server() { grep "${AUDIODB}" /proc/$1/cmdline > /dev/null kill $1 # HACK: deal with race on process exit sleep 1 expect_clean_error_exit grep ${AUDIODB} /proc/$1/cmdline } check_server() { grep "${AUDIODB}" /proc/$1/cmdline > /dev/null } expect_client_failure() { # FIXME: work out whether and how the client should report server # errors. At present, the client exits with a zero exit code. "$@" }