Commit 0bd73495 authored by mats@mysql.com's avatar mats@mysql.com

Merge mysql.com:/home/bkroot/mysql-5.1-new

into  mysql.com:/home/bk/merge-mysql-5.1-wl3206
parents 86d6b161 02555385
......@@ -29,7 +29,7 @@ SUBDIRS = . include @docs_dirs@ @zlib_dir@ @yassl_dir@ \
@mysql_se_plugins@ \
netware @libmysqld_dirs@ \
@bench_dirs@ support-files @tools_dirs@ \
plugin win
plugin unittest win
DIST_SUBDIRS = . include @docs_dirs@ zlib \
@readline_topdir@ sql-common \
......@@ -39,7 +39,7 @@ DIST_SUBDIRS = . include @docs_dirs@ zlib \
@man_dirs@ tests \
BUILD netware @libmysqld_dirs@\
@bench_dirs@ support-files server-tools \
plugin win
plugin unittest win
# Run these targets before any others, also make part of clean target,
# to make sure we create new links after a clean.
......
......@@ -2586,6 +2586,9 @@ AC_SUBST(MAKE_BINARY_DISTRIBUTION_OPTIONS)
# Output results
AC_CONFIG_FILES(Makefile extra/Makefile mysys/Makefile dnl
unittest/Makefile dnl
unittest/mytap/Makefile unittest/mytap/t/Makefile dnl
unittest/mysys/Makefile unittest/examples/Makefile dnl
strings/Makefile regex/Makefile storage/Makefile storage/heap/Makefile dnl
storage/myisam/Makefile storage/myisammrg/Makefile dnl
man/Makefile BUILD/Makefile vio/Makefile dnl
......
......@@ -60,6 +60,8 @@ extern void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size);
extern void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
extern void bitmap_invert(MY_BITMAP *map);
extern uint bitmap_lock_set_next(MY_BITMAP *map);
extern void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit);
......
SUBDIRS = mytap . mysys examples
noinst_SCRIPTS = unit
unittests = mysys examples
.PHONY: all mytap mysys examples test
test: unit all
@./unit run $(unittests)
mytap:
cd mytap && $(MAKE)
mysys:
cd mysys && $(MAKE)
examples:
cd examples && $(MAKE)
unit: unit.pl
cp $< $@
chmod +x $@
Unit tests directory structure
------------------------------
This is the current structure of the unit tests. More directories
will be added over time.
mytap Source for the MyTAP library
mysys Tests for mysys components
bitmap.t.c Unit test for MY_BITMAP
base64.t.c Unit test for base64 encoding functions
examples Example unit tests
simple.t.c Example of a standard TAP unit test
skip.t.c Example where some test points are skipped
skip_all.t.c Example of a test where the entire test is skipped
todo.t.c Example where test contain test points that are TODO
no_plan.t.c Example of a test with no plan (avoid this)
Executing unit tests
--------------------
To make and execute all unit tests in the directory:
make test
Adding unit tests
-----------------
Add a file with a name of the format "foo.t.c" to the appropriate
directory and add the following to the Makefile.am in that directory
(where ... denotes stuff already there):
noinst_PROGRAMS = ... foo.t
foo_t_c_SOURCES = foo.t.c
AM_CPPFLAGS = -I$(srcdir) -I$(top_builddir)/include
AM_CPPFLAGS += -I$(top_builddir)/unittest/mytap
AM_LDFLAGS = -L$(top_builddir)/unittest/mytap
AM_CFLAGS = -W -Wall -ansi -pedantic
LDADD = -lmytap
noinst_PROGRAMS = simple.t skip.t todo.t skip_all.t no_plan.t
simple_t_SOURCES = simple.t.c
skip_t_SOURCES = skip.t.c
todo_t_SOURCES = todo.t.c
skip_all_t_SOURCES = skip_all.t.c
no_plan_t_SOURCES = no_plan.t.c
#include <stdlib.h>
#include <tap.h>
/*
Sometimes, the number of tests is not known beforehand. In those
cases, the plan can be omitted and will instead be written at the
end of the test (inside exit_status()).
Use this sparingly, it is a last resort: planning how many tests you
are going to run will help you catch that offending case when some
tests are skipped for an unknown reason.
*/
int main() {
ok(1, NULL);
ok(1, NULL);
ok(1, NULL);
return exit_status();
}
#include <tap.h>
unsigned int gcs(unsigned int a, unsigned int b)
{
if (b > a) {
unsigned int t = a;
a = b;
b = t;
}
while (b != 0) {
unsigned int m = a % b;
a = b;
b = m;
}
return a;
}
int main() {
unsigned int a,b;
unsigned int failed;
plan(1);
diag("Testing basic functions");
failed = 0;
for (a = 1 ; a < 2000 ; ++a)
for (b = 1 ; b < 2000 ; ++b)
{
unsigned int d = gcs(a, b);
if (a % d != 0 || b % d != 0) {
++failed;
diag("Failed for gcs(%4u,%4u)", a, b);
}
}
ok(failed == 0, "Testing gcs()");
return exit_status();
}
#include <tap.h>
#include <stdlib.h>
int main() {
plan(4);
ok(1, NULL);
ok(1, NULL);
SKIP_BLOCK_IF(1, 2, "Example of skipping a few test points in a test") {
ok(1, NULL);
ok(1, NULL);
}
return exit_status();
}
#include <stdlib.h>
#include <tap.h>
int has_feature() {
return 0;
}
/*
In some cases, an entire test file does not make sense because there
some feature is missing. In that case, the entire test case can be
skipped in the following manner.
*/
int main() {
if (!has_feature())
skip_all("Example of skipping an entire test");
plan(4);
ok(1, NULL);
ok(1, NULL);
ok(1, NULL);
ok(1, NULL);
return exit_status();
}
#include <stdlib.h>
#include <tap.h>
int main()
{
plan(4);
ok(1, NULL);
ok(1, NULL);
/*
Tests in the todo region is expected to fail. If they don't,
something is strange.
*/
todo_start("Need to fix these");
ok(0, NULL);
ok(0, NULL);
todo_end();
return exit_status();
}
AM_CPPFLAGS = @ZLIB_INCLUDES@ -I$(top_builddir)/include
AM_CPPFLAGS += -I$(top_srcdir)/include -I$(top_builddir)/unittest/mytap
AM_LDFLAGS = -L$(top_builddir)/unittest/mytap -L$(top_builddir)/mysys
AM_LDFLAGS += -L$(top_builddir)/strings -L$(top_builddir)/dbug
LDADD = -lmytap -lmysys -lmystrings -ldbug
noinst_PROGRAMS = bitmap.t base64.t
bitmap_t_SOURCES = bitmap.t.c
base64_t_SOURCES = base64.t.c
/* Copyright (C) 2003 MySQL AB
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.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include <base64.h>
#include <tap.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
int i;
size_t j, k, l, dst_len, needed_length;
for (i= 0; i < 500; i++)
{
/* Create source data */
const size_t src_len= rand() % 1000 + 1;
char * src= (char *) malloc(src_len);
char * s= src;
char * str;
char * dst;
for (j= 0; j<src_len; j++)
{
char c= rand();
*s++= c;
}
/* Encode */
needed_length= base64_needed_encoded_length(src_len);
str= (char *) malloc(needed_length);
for (k= 0; k < needed_length; k++)
str[k]= 0xff; /* Fill memory to check correct NUL termination */
ok(base64_encode(src, src_len, str) == 0,
"base64_encode: size %d", i);
ok(needed_length == strlen(str) + 1,
"base64_needed_encoded_length: size %d", i);
/* Decode */
dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
dst_len= base64_decode(str, strlen(str), dst);
ok(dst_len == src_len, "Comparing lengths");
int cmp= memcmp(src, dst, src_len);
ok(cmp == 0, "Comparing encode-decode result");
if (cmp != 0)
{
diag(" --------- src --------- --------- dst ---------");
char buf[80];
for (k= 0; k<src_len; k+=8)
{
sprintf(buf, "%.4x ", (uint) k);
for (l=0; l<8 && k+l<src_len; l++)
{
unsigned char c= src[k+l];
sprintf(buf, "%.2x ", (unsigned)c);
}
sprintf(buf, " ");
for (l=0; l<8 && k+l<dst_len; l++)
{
unsigned char c= dst[k+l];
sprintf(buf, "%.2x ", (unsigned)c);
}
diag(buf);
}
diag("src length: %.8x, dst length: %.8x\n",
(uint) src_len, (uint) dst_len);
}
}
return exit_status();
}
This diff is collapsed.
This diff is collapsed.
AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include -I$(srcdir)
noinst_LIBRARIES = libmytap.a
noinst_HEADERS = tap.h
libmytap_a_SOURCES = tap.c
SUBDIRS = . t
AM_CPPFLAGS = -I$(srcdir) -I$(top_builddir)/include
AM_CPPFLAGS += -I$(srcdir)/..
AM_LDFLAGS = -L$(srcdir)/..
AM_CFLAGS = -Wall -ansi -pedantic
LDADD = -lmytap
noinst_PROGRAMS = basic.t
basic_t_SOURCES = basic.t.c
all: $(noinst_PROGRAMS)
#include <stdlib.h>
#include <tap.h>
int main() {
plan(5);
ok(1 == 1, "testing basic functions");
ok(2 == 2, "");
ok(3 == 3, NULL);
if (1 == 1)
skip(2, "Sensa fragoli");
else {
ok(1 == 2, "Should not be run at all");
ok(1, "This one neither");
}
return exit_status();
}
/* Copyright (C) 2006 MySQL AB
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Library for providing TAP support for testing C and C++ was written
by Mats Kindahl <mats@mysql.com>.
*/
#include "tap.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/**
Test data structure.
Data structure containing all information about the test suite.
@ingroup MyTAP
*/
static TEST_DATA g_test = { 0, 0, 0, "" };
/**
Output stream for test report message.
The macro is just a temporary solution.
*/
#define tapout stdout
/**
Emit the beginning of a test line, that is: "(not) ok", test number,
and description.
To emit the directive, use the emit_dir() function
@ingroup MyTAP
@see emit_dir
@param pass 'true' if test passed, 'false' otherwise
@param fmt Description of test in printf() format.
@param ap Vararg list for the description string above.
*/
static void
emit_tap(int pass, char const *fmt, va_list ap)
{
fprintf(tapout, "%sok %d%s",
pass ? "" : "not ",
++g_test.last,
(fmt && *fmt) ? " - " : "");
if (fmt && *fmt)
vfprintf(tapout, fmt, ap);
}
/**
Emit a TAP directive.
TAP directives are comments after a have the form
@code
ok 1 # skip reason for skipping
not ok 2 # todo some text explaining what remains
@endcode
@param dir Directive as a string
@param exp Explanation string
*/
static void
emit_dir(const char *dir, const char *exp)
{
fprintf(tapout, " # %s %s", dir, exp);
}
/**
Emit a newline to the TAP output stream.
*/
static void
emit_endl()
{
fprintf(tapout, "\n");
}
void
diag(char const *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(tapout, "# ");
vfprintf(tapout, fmt, ap);
fprintf(tapout, "\n");
va_end(ap);
}
void
plan(int const count)
{
g_test.plan= count;
switch (count)
{
case NO_PLAN:
break;
default:
if (count > 0)
fprintf(tapout, "1..%d\n", count);
break;
}
}
void
skip_all(char const *reason, ...)
{
va_list ap;
va_start(ap, reason);
fprintf(tapout, "1..0 # skip ");
vfprintf(tapout, reason, ap);
va_end(ap);
exit(0);
}
void
ok(int const pass, char const *fmt, ...)
{
if (!pass && *g_test.todo == '\0')
++g_test.failed;
va_list ap;
va_start(ap, fmt);
emit_tap(pass, fmt, ap);
va_end(ap);
if (*g_test.todo != '\0')
emit_dir("todo", g_test.todo);
emit_endl();
}
void
skip(int how_many, char const *const fmt, ...)
{
char reason[80];
if (fmt && *fmt)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(reason, sizeof(reason), fmt, ap);
va_end(ap);
}
else
reason[0] = '\0';
while (how_many-- > 0)
{
va_list ap;
emit_tap(1, NULL, ap);
emit_dir("skip", reason);
emit_endl();
}
}
void
todo_start(char const *message, ...)
{
va_list ap;
va_start(ap, message);
vsnprintf(g_test.todo, sizeof(g_test.todo), message, ap);
va_end(ap);
}
void
todo_end()
{
*g_test.todo = '\0';
}
int exit_status() {
/*
If there were no plan, we write one last instead.
*/
if (g_test.plan == NO_PLAN)
plan(g_test.last);
if (g_test.plan != g_test.last)
{
diag("%d tests planned but only %d executed",
g_test.plan, g_test.last);
return EXIT_FAILURE;
}
if (g_test.failed > 0)
{
diag("Failed %d tests!", g_test.failed);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
@mainpage Testing C and C++ using MyTAP
@section IntroSec Introduction
Unit tests are used to test individual components of a system. In
contrast, functional tests usually test the entire system. The
rationale is that each component should be correct if the system is
to be correct. Unit tests are usually small pieces of code that
tests an individual function, class, a module, or other unit of the
code.
Observe that a correctly functioning system can be built from
"faulty" components. The problem with this approach is that as the
system evolves, the bugs surface in unexpected ways, making
maintenance harder.
The advantages of using unit tests to test components of the system
are several:
- The unit tests can make a more thorough testing than the
functional tests by testing correctness even for pathological use
(which shouldn't be present in the system). This increases the
overall robustness of the system and makes maintenance easier.
- It is easier and faster to find problems with a malfunctioning
component than to find problems in a malfunctioning system. This
shortens the compile-run-edit cycle and therefore improves the
overall performance of development.
- The component has to support at least two uses: in the system and
in a unit test. This leads to more generic and stable interfaces
and in addition promotes the development of reusable components.
For example, the following are typical functional tests:
- Does transactions work according to specifications?
- Can we connect a client to the server and execute statements?
In contrast, the following are typical unit tests:
- Can the 'String' class handle a specified list of character sets?
- Does all operations for 'my_bitmap' produce the correct result?
- Does all the NIST test vectors for the AES implementation encrypt
correctly?
@section UnitTest Writing unit tests
The purpose of writing unit tests is to use them to drive component
development towards a solution that the tests. This means that the
unit tests has to be as complete as possible, testing at least:
- Normal input
- Borderline cases
- Faulty input
- Error handling
- Bad environment
We will go over each case and explain it in more detail.
@subsection NormalSSec Normal input
@subsection BorderlineSSec Borderline cases
@subsection FaultySSec Faulty input
@subsection ErrorSSec Error handling
@subsection EnvironmentSSec Environment
Sometimes, modules has to behave well even when the environment
fails to work correctly. Typical examples are: out of dynamic
memory, disk is full,
@section UnitTestSec How to structure a unit test
In this section we will give some advice on how to structure the
unit tests to make the development run smoothly.
@subsection PieceSec Test each piece separately
Don't test all functions using size 1, then all functions using
size 2, etc.
*/
/* Copyright (C) 2006 MySQL AB
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Library for providing TAP support for testing C and C++ was written
by Mats Kindahl <mats@mysql.com>.
*/
#ifndef TAP_H
#define TAP_H
#ifndef __GNUC__
#define __attribute__(F)
#endif
/*
@defgroup MyTAP MySQL support for performing unit tests according to TAP.
*/
#define NO_PLAN (0)
/**
Data about test plan.
@internal We are using the "typedef struct X { ... } X" idiom to
create class/struct X both in C and C++.
*/
typedef struct TEST_DATA {
/**
Number of tests that is planned to execute.
Can be zero (<code>NO_PLAN</code>) meaning that the plan string
will be printed at the end of test instead.
*/
int plan;
/** Number of last test that was done or skipped. */
int last;
/** Number of tests that failed. */
int failed;
/** Todo reason. */
char todo[128];
} TEST_DATA;
#ifdef __cplusplus
extern "C" {
#endif
/**
Set number of tests that is planned to execute.
The function also accepts the predefined constant
<code>NO_PLAN</code>. If the function is not called, it is as if
it was called with <code>NO_PLAN</code>, i.e., the test plan will
be printed after all the test lines.
@param count The planned number of tests to run.
*/
void plan(int count);
/**
Report test result as a TAP line.
Function used to write status of an individual test. Call this
function in the following manner:
@code
ok(ducks == paddling,
"%d ducks did not paddle", ducks - paddling);
@endcode
@param pass Zero if the test failed, non-zero if it passed.
@param fmt Format string in printf() format. NULL is allowed, in
which case nothing is printed.
*/
void ok(int pass, char const *fmt, ...)
__attribute__((format(printf,2,3)));
/**
Skip a determined number of tests.
Function to print that <em>how_many</em> tests have been
skipped. The reason is printed for each skipped test. Observe
that this function does not do the actual skipping for you, it just
prints information that tests have been skipped. It shall be used
in the following manner:
@code
if (ducks == 0) {
skip(2, "No ducks in the pond");
} else {
int i;
for (i = 0 ; i < 2 ; ++i)
ok(duck[i] == paddling, "is duck %d paddling?", i);
}
@endcode
@see SKIP_BLOCK_IF
@param how_many Number of tests that are to be skipped.
@param reason A reason for skipping the tests
*/
void skip(int how_many, char const *reason, ...)
__attribute__((format(printf,2,3)));
/**
Helper macro to skip a block of code. The macro can be used to
simplify conditionally skipping a block of code. It is used in the
following manner:
@code
SKIP_BLOCK_IF(ducks == 0, 2, "No ducks in the pond")
{
int i;
for (i = 0 ; i < 2 ; ++i)
ok(duck[i] == paddling, "is duck %d paddling?", i);
}
@see skip
@endcode
*/
#define SKIP_BLOCK_IF(SKIP_IF_TRUE, COUNT, REASON) \
if (SKIP_IF_TRUE) skip((COUNT),(REASON)); else
/**
Print a diagnostics message.
@param fmt Diagnostics message in printf() format.
*/
void diag(char const *fmt, ...)
__attribute__((format(printf,1,2)));
/**
Print summary report and return exit status.
This function will print a summary report of how many tests passed,
how many were skipped, and how many remains to do. The function
should be called after all tests are executed in the following
manner:
@code
return exit_status();
@endcode
@returns EXIT_SUCCESS if all tests passed, EXIT_FAILURE if one or
more tests failed.
*/
int exit_status(void);
/**
Skip entire test suite.
To skip the entire test suite, use this function. It will
automatically call exit(), so there is no need to have checks
around it.
*/
void skip_all(char const *reason, ...)
__attribute__((noreturn, format(printf, 1, 2)));
/**
Start section of tests that are not yet ready.
To start a section of tests that are not ready and are expected to
fail, use this function and todo_end() in the following manner:
@code
todo_start("Not ready yet");
ok(is_rocketeering(duck), "Rocket-propelled ducks");
ok(is_kamikaze(duck), "Kamikaze ducks");
todo_end();
@endcode
@see todo_end
@note
It is not possible to nest todo sections.
@param message Message that will be printed before the todo tests.
*/
void todo_start(char const *message, ...)
__attribute__((format (printf, 1, 2)));
/**
End a section of tests that are not yet ready.
*/
void todo_end();
#ifdef __cplusplus
}
#endif
#endif /* TAP_H */
#!/usr/bin/perl
# Override _command_line in the standard Perl test harness to prevent
# it from using "perl" to run the test scripts.
package MySQL::Straps;
use base qw(Test::Harness::Straps);
use strict;
sub _command_line {
return $_[1]
}
package main;
use Test::Harness qw(&runtests $verbose);
use File::Find;
use strict;
sub run_cmd (@);
my %dispatch = (
"run" => \&run_cmd,
);
=head1 NAME
unit - Run unit tests in directory
=head1 SYNOPSIS
unit run
=cut
my $cmd = shift;
# $Test::Harness::Verbose = 1;
# $Test::Harness::Debug = 1;
if (defined $cmd && exists $dispatch{$cmd}) {
$dispatch{$cmd}->(@ARGV);
} else {
print "Unknown command", (defined $cmd ? " $cmd" : ""), ".\n";
print "Available commands are: ", join(", ", keys %dispatch), "\n";
}
=head2 run
Run all unit tests in the current directory and all subdirectories.
=cut
sub _find_test_files (@) {
my @dirs = @_;
my @files;
find sub {
$File::Find::prune = 1 if /^SCCS$/;
push(@files, $File::Find::name) if -x _ && /\.t\z/;
}, @dirs;
return @files;
}
sub run_cmd (@) {
my @files;
# If no directories were supplied, we add all directories in the
# current directory except 'mytap' since it is not part of the
# test suite.
if (@_ == 0) {
# Ignore these directories
my @ignore = qw(mytap SCCS);
# Build an expression from the directories above that tests if a
# directory should be included in the list or not.
my $ignore = join(' && ', map { '$_ ne ' . "'$_'"} @ignore);
# Open and read the directory. Filter out all files, hidden
# directories, and directories named above.
opendir(DIR, ".") or die "Cannot open '.': $!\n";
@_ = grep { -d $_ && $_ !~ /^\..*/ && eval $ignore } readdir(DIR);
closedir(DIR);
}
print "Running tests: @_\n";
foreach my $name (@_) {
push(@files, _find_test_files $name) if -d $name;
push(@files, $name) if -f $name;
}
if (@files > 0) {
# Removing the first './' from the file names
foreach (@files) { s!^\./!! }
# Install the strap above instead of the default strap. Since
# we are replacing the straps under the feet of Test::Harness,
# we need to do some basic initializations in the new straps.
$Test::Harness::Strap = MySQL::Straps->new;
$Test::Harness::Strap->{callback} = \&Test::Harness::strap_callback
if defined &Test::Harness::strap_callback;
runtests @files;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment