Skip to content

Commit

Permalink
passed unit-test for buff and callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mathetian committed Jun 18, 2014
1 parent 7deec12 commit ffd169f
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ utils/SQueue
utils/Atomic
utils/Buffer
utils/Thread
utils/C
utils/Header
utils/Noncopyable
utils/Tester

*.orig
*.log
Expand Down
7 changes: 6 additions & 1 deletion Make.defines
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ CLEAN = \
utils/Slice \
utils/Utils \
utils/Timer \
utils/C \
utils/Header \
utils/Noncopyable \
utils/Tester \
client \
server \
tests/test_callback \
Expand All @@ -25,4 +29,5 @@ CLEAN = \
utils/Atomic \
utils/Buffer \
utils/Thread \
include/MsgHandler
include/MsgHandler \
*.a
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ include Make.defines

CXX = g++
AR = ar
LIBMISC = libcustomserver.a
LIBMISC = libsealedserver.a
RANLIB = ranlib
HEADER = -I./include -I. -I./utils
HEADER = -I. -I./include -I./utils
CXXFLAGS = -g -O0
PTHRFLAGS = -lpthread -pthread

SOURCES = cache/*.cpp core/*.cpp helpers/*.cpp dbimpl/*/*.cpp utils/*.cpp
SOURCES = utils/*.cpp
LDLIBS = -L. -lsealedserver

tests = test_squeue test_buffer test_callback test_log test_slice test_tostring test_thread

PROGS = server client bench_library ${tests}

all: clean prepare ${PROGS}
lib: clean prepare compile
${AR} rv ${LIBMISC} *.o
${RANLIB} ${LIBMISC}
rm *.o

compile:
${CXX} ${CXXFLAGS} ${HEADER} -lpthread -c ${SOURCES}

echo: clean prepare server client bench_library

Expand All @@ -35,11 +42,11 @@ test_squeue: tests/test_squeue.cpp
mv $@ bin

test_buffer: tests/test_buffer.cpp
$(CXX) ${CXXFLAGS} ${HEADER} ${PTHRFLAGS} $^ -o $@
$(CXX) ${CXXFLAGS} ${HEADER} ${PTHRFLAGS} $^ -o $@ ${LDLIBS}
mv $@ bin

test_callback: tests/test_callback.cpp
$(CXX) ${CXXFLAGS} ${HEADER} ${PTHRFLAGS} $^ -o $@
$(CXX) ${CXXFLAGS} ${HEADER} ${PTHRFLAGS} $^ -o $@ ${LDLIBS}
mv $@ bin

test_log: tests/test_log.cpp utils/Log.cpp
Expand Down
65 changes: 56 additions & 9 deletions tests/test_buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
#include <string>
#include <iostream>
using namespace std;
// Copyright (c) 2014 The SealedServer Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "Buffer.h"
#include "Tester.h"
using namespace utils;

void TestBuffer(const Buffer buf)
class A
{
Buffer buf;
public:
A() : buf("hello world") { }

int size() { return buf.size(); }
};

/**
** Test for size
**/
TEST(A, Size)
{
cout<<"len2:"<<buf.length()<<endl;
}
ASSERT_EQ(size(), 11);
};

/**
** Test for counter
** Expect behavior: release/output/release/release
**/
TEST(A, Counter)
{
Buffer buff;

{
Buffer buff1("hello world");
buff = buff1;
}

cout << (string)buff << endl;
};

/**
** Test for counter
** Expect behavior: release/output/release/release
**/
TEST(A, SelfAlloc)
{
Buffer buff;

{
char * str = new char[10];
memset(str, 0, 10);
strncpy(str, "great day", 9);

Buffer buff1(str, true);
buff = buff1;
}

cout << (string)buff << endl;
};

int main()
{
Buffer buf("hello world");
cout<<"len1:"<<buf.length()<<endl;
TestBuffer(buf);
RunAllTests();
return 0;
}
56 changes: 49 additions & 7 deletions tests/test_callback.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
#include <stdio.h>
// Copyright (c) 2014 The SealedServer Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "Tester.h"
#include "Callback.h"
using namespace utils;

class A
class A
{

};

class B
{
public:
void a()
void b()
{
printf("hello, enter function a\n");
printf("hello, enter function b\n");
}

~B()
{
printf("release B\n");
}
};

int main()
TEST(A, callfromb)
{
B b1;
Callback<void> call(&b1, &B::b);
call();
}

TEST(A, equal)
{
A a1;
Callback<void> call(a1, &A::a);
Callback<void> call;

B b1;
call = Callback<void>(&b1, &B::b);
call();
}

/**
** Should throw exception, bug
**/
TEST(A, null)
{
Callback<void> call;

{
B b1;
call = Callback<void>(&b1, &B::b);
}

call();
}

int main()
{
RunAllTests();
return 0;
}
25 changes: 23 additions & 2 deletions tests/test_log.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
#include <stdint.h>
// Copyright (c) 2014 The SealedServer Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "Log.h"
#include "Tester.h"
using namespace utils;

class A { };

class B
{
public:
};

TEST(A, Plain)
{
int a = 3;
DEBUG << a;
}

Test(A, Object)
{

}

int main()
{
uint32_t a = 3;

DEBUG << a;
INFO << 31 << " " << 31;
return 0;
Expand Down
17 changes: 10 additions & 7 deletions utils/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class IMutableBuff
return len;
}

unsigned int size() const
{
return length();
}

operator const void *() const
{
return dat;
Expand Down Expand Up @@ -109,7 +114,7 @@ class MutableBuff : public IMutableBuff
class Buffer : public MutableBuff
{
Atomic *ref;
int self_alloc;
bool self_alloc;

private:
void acquire()
Expand All @@ -119,7 +124,7 @@ class Buffer : public MutableBuff

void release()
{
if(self_alloc == 0) return;
if(self_alloc == false) return;
if (ref && (ref->addAndGet(-1)) == 0)
{
delete[] dat;
Expand All @@ -130,28 +135,26 @@ class Buffer : public MutableBuff

public:
/// Null constructor.
Buffer() : MutableBuff(), ref(0)
Buffer() : MutableBuff(), ref(0), self_alloc(false)
{
acquire();
self_alloc = 0;
}

/// Dynamically allocates a data buffer whose current length is
/// 0 bytes but whose maximum length is <i>maxlen</i> bytes.
/// The underlying storage will be deallocate when the last
/// Buffer referring to that storage is destructed.
Buffer(unsigned int maxlen) : MutableBuff(new char[maxlen], 0, maxlen), \
ref(new Atomic(0)), self_alloc(1)
ref(new Atomic(0)), self_alloc(true)
{
acquire();
zeros();
}

Buffer(const char* str, int self_alloc = 0) : MutableBuff(str, strlen(str), strlen(str) + 1), \
Buffer(const char* str, bool self_alloc = false) : MutableBuff(str, strlen(str), strlen(str)), \
ref(new Atomic(0)), self_alloc(self_alloc)
{
acquire();
if(self_alloc == 1) zeros();
}

Buffer(const string &str) : MutableBuff(str.data(), str.size(), str.size()), ref(new Atomic(0)), self_alloc(0)
Expand Down
2 changes: 2 additions & 0 deletions utils/C.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef _C_H
#define _C_H

#include "Header.h"

/**
** C.h provides some C-like helper functions
**
Expand Down
28 changes: 15 additions & 13 deletions utils/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef _BIND_H
#define _BIND_H

#include "Header.h"

namespace utils
{

Expand Down Expand Up @@ -92,8 +94,8 @@ class Callback
};

func *m_fn;
public:

public:
R operator() () const
{
assert(m_fn);
Expand All @@ -109,36 +111,36 @@ class Callback
struct bindfunc : public func
{
typedef R(O::*fp_t)();
fp_t fp;
O& o;
bindfunc(O& o, fp_t fp) : o(o), fp(fp)
fp_t fp; O* op;

bindfunc(O* op, fp_t fp) : op(op), fp(fp)
{
assert(&o);
assert(op);
assert(fp);
}

R operator() ()
{
return (o.*fp)();
assert(op);
return (op->*fp)();
}

bool stat()
{
if(&o == NULL)
if(op == NULL)
return false;
return true;
}
};

template<typename O>
static func* init( O& o, R(O::*fp)() )
static func* init( O* op, R(O::*fp)() )
{
return new bindfunc<O>(o, fp);
return new bindfunc<O>(op, fp);
}

template<typename O>
Callback( O& o, R(O::*fp)() ) : m_fn( init(o, fp) ) {}

template<typename O>
Callback( O* o, R(O::*fp)() ) : m_fn( init(*o, fp) ) {}
Callback( O* op, R(O::*fp)() ) : m_fn( init(op, fp) ) {}

Callback() : m_fn(NULL) { }
};
Expand Down
Loading

0 comments on commit ffd169f

Please sign in to comment.