Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 106 lines (87 sloc) 2.223 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/**
* Copyright (C) 2005-2008 Christoph Rupp (chris@crupp.de).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See files COPYING.* for License information.
*/

#include <stdexcept>
#include <cstring>
#include <cassert>
#include <ham/hamsterdb_int.h>
#include "../src/error.h"

#include "bfc-testsuite.hpp"
#include "hamster_fixture.hpp"

using namespace bfc;

static void HAM_CALLCONV
my_handler(int level, const char *msg)
{
    static int i=0;
    static const char *s[]={
        "hello world",
        "ham_verify test 1",
        "(none)",
        "hello world 42",
    };
    const char *p=strstr(msg, ": ");
    if (!p)
        return;
    p+=2;
    assert(0==::strcmp(s[i], p));
    i++;
}

static int g_aborted=0;

static void
my_abort_handler(void)
{
    g_aborted=1;
}

class ErrorTest : public hamsterDB_fixture
{
define_super(hamsterDB_fixture);

public:
    ErrorTest()
        : hamsterDB_fixture("ErrorTest")
    {
        testrunner::get_instance()->register_fixture(this);
        BFC_REGISTER_TEST(ErrorTest, errorHandlerTest);
        BFC_REGISTER_TEST(ErrorTest, verifyTest);
    }

public:
    virtual void setup()
{
__super::setup();

        ham_set_errhandler(my_handler);
}

    virtual void teardown()
{
__super::teardown();

ham_set_errhandler(0);
}

    void errorHandlerTest()
    {
        ham_trace(("hello world"));
        ham_set_errhandler(0);
        ham_log(("testing error handler - hello world\n"));
    }

    void verifyTest()
    {
        ham_test_abort=my_abort_handler;

        g_aborted=0;
        ham_verify(0, ("ham_verify test 1"));
        BFC_ASSERT_EQUAL(1, g_aborted);
        g_aborted=0;
        ham_verify(1, ("ham_verify test 2"));
        BFC_ASSERT_EQUAL(0, g_aborted);
        g_aborted=0;
        ham_verify(!"expr", (0));
        BFC_ASSERT_EQUAL(1, g_aborted);
        ham_verify(!"expr", ("hello world %d", 42));
        BFC_ASSERT_EQUAL(1, g_aborted);

        ham_test_abort=0;
    }
};

BFC_REGISTER_FIXTURE(ErrorTest);

Something went wrong with that request. Please try again.