2011年8月24日水曜日

homebrewでvalgrindが入らない。

# brew install valrgind
ってすると、
configure:5116: checking for the kernel version
configure:5152: result: unsupported (11.0.1)
configure:5154: error: Valgrind works on Darwin 9.x and 10.x (Mac OS X 10.5 and 10.6)
ってなるの。

コイツめ。
# brew info valgrind
valgrind 3.6.1
http://www.valgrind.org/
Depends on: pkg-config
http://github.com/mxcl/homebrew/commits/master/Library/Formula/valgrind.rb

gccで定義済みとなるマクロさん達

# cpp -dM /dev/null
すれば良いみたいよ。

2011年8月15日月曜日

stl::vectorのパフォーマンス

いつも適当に書いてしまうので、ちょっとまとめておく。
  • 大きさを指定せずに push_back し続ける。
  • コンストラクトするときに大きさを指定して添字で代入する。
  • reserveで大きさを指定して、push_backする。
のバトル。

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/timer.hpp>

struct E {
    unsigned int id;
    unsigned char value[1024];
};

struct NonOptimize {
    NonOptimize(const size_t num) : n(num) { }

    inline void operator()() const {
        std::vector<E> v;
        for (size_t i = 0; i < n; ++i) {
            E e = {i};
            v.push_back(e);
        }
    }

    const size_t n;
};

struct ConstructWithSize {
    ConstructWithSize(const size_t num) : n(num) { }

    inline void operator()() const {
        std::vector<E> v(n);
        for (size_t i = 0; i < n; ++i) {
            E e = {i};
            v[i] = e;
        }
    }

    const size_t n;
};

struct Reserved {
    Reserved(const size_t num) : n(num) { }

    inline void operator()() const {
        std::vector<E> v;
        v.reserve(n);
        for (size_t i = 0; i < n; ++i) {
            E e = {i};
            v.push_back(e);
        }
    }

    const size_t n;
};

template <typename F>
inline std::vector<double>& mesure(F& func, std::vector<double>* elapsed) {
    typedef std::vector<double> Results;
    boost::timer timer;
    
    for (Results::iterator i(elapsed->begin()), end(elapsed->end()); i != end; ++i) {
        timer.restart();
        func();
        *i = timer.elapsed();
    }
    return *elapsed;
}

std::ostream& operator<<(std::ostream& os, const std::vector<double>& elapsed) {
    typedef std::vector<double> Results;
    Results::const_iterator max = std::max_element(elapsed.begin(), elapsed.end());
    Results::const_iterator min = std::min_element(elapsed.begin(), elapsed.end());
    double sum(0);;
    for (Results::const_iterator i(elapsed.begin()), end(elapsed.end()); i != end; ++i) {
        if (i != max && i != min) {
            sum += *i;
        }
    }
    os << "MAX: " << *max << ", MIN:" << *min << ", AVG:" << (sum / (elapsed.size() - 2));
    return os;
}

int main(int argc, char **argv) {
    static const size_t NUM(50000);
    boost::timer timer;

    NonOptimize f1(NUM);
    ConstructWithSize f2(NUM);
    Reserved f3(NUM);

    std::cout << argv[0] << std::endl;
    std::vector<double> elapsed(10);
    std::cout << mesure(f1, &elapsed) << std::endl;
    std::cout << mesure(f2, &elapsed) << std::endl;
    std::cout << mesure(f3, &elapsed) << std::endl;

    return 0;
}
で、ビルド。ついでだから llvm でもお試し。
all: main_gnu main_llvm_gnu

main_gnu: main.cc
	g++ $(CXXFLAGS) $< -o $@

main_llvm_gnu: main.cc
	llvm-g++ $(CXXFLAGS) $< -o $@

.PHONY: clean
clean:
	@rm -fv main_gnu main_llvm_gnu

したら、実行。
$ make; ./main_gnu; ./main_llvm_gnu
g++ -O3 -Wall main.cc -o main_gnu
llvm-g++ -O3 -Wall main.cc -o main_llvm_gnu
./main_gnu
MAX: 0.29, MIN:0.26, AVG:0.27875
MAX: 0.18, MIN:0.14, AVG:0.15125
MAX: 0.13, MIN:0.11, AVG:0.1175
./main_llvm_gnu
MAX: 0.25, MIN:0.24, AVG:0.2425
MAX: 0.15, MIN:0.13, AVG:0.13875
MAX: 0.11, MIN:0.1, AVG:0.105

  • 大きさを指定せずに push_back し続ける。
  • コンストラクトするときに大きさを指定して添字で代入する。
  • reserveで大きさを指定して、push_backする。
コンストラクタで大きさを指定した場合、アロケーションしてから、初期値を代入してるんだろうな。
任意値で埋めたいときは、reserveしたほうが吉っぽい。

2011年8月8日月曜日

lionのツールチェイン

ファイル 正体
/usr/bin/ld llvm version 3.0svn, from Apple Clang 2.1 (build 163.7.1)
/usr/bin/as Apple Inc version cctools-806~3, GNU assembler version 1.38
/usr/bin/gcc symbolic link to llvm-gcc-4.2
/usr/bin/llvm-gcc symbolic link to ../llvm-gcc-4.2/bin/llvm-gcc-4.2
/usr/bin/llvm-gcc-4.2 symbolic link to ../llvm-gcc-4.2/bin/llbm-gcc-4.2
/usr/bin/gcc-4.2 gcc version 4.2.1 (Appli Inc. build 5666) (dot 3)
/usr/bin/llvm-g++ symbolic link to ../llvm-gcc-4.2/bin/llvm-g++-4.2
/usr/bin/llvm-g++-4.2 symbolic link to ../llvm-gcc-4.2/bin/llvm-g++-4.2
/usr/bin/llvm-cpp-4.2 symbolic link to ../llvm-gcc-4.2/bin/llvm-cpp-4.2
/usr/bin/g++ symbolic link to llvm-g++-4.2
/usr/bin/g++-4.2 i686-apple-darwin11-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)

2011年7月7日木曜日

Overloaded methods is ambiguous.

オーバーロードしている関数で引数の数まで同じだと、悲しいことになる。

例えば、
#include <gmock/gmock.h>

using ::testing::Eq;
using ::testing::Return;
using ::testing::Matcher;

class X {
public:
    bool operator==(const X& other) const { return false; }
};

class Y {
public:
    bool operator==(const Y& other) const { return false; }
};

class Foo {
public:
    virtual ~Foo() { }

    virtual void function(X x) { }
    virtual void function(Y x) { }
};

class Mock : public Foo {
public:
    MOCK_METHOD1(function, void(X));
    MOCK_METHOD1(function, void(Y));
};

TEST(MockTest, Call_Foo_X) {
    Mock m;

    X arg;

    EXPECT_CALL(m, function(Eq(arg))).WillOnce(Return());
}

これをコンパイルすると
$ g++ -I/opt/vendor/include -L/opt/vendor/lib -lgtest -lgmock -c ambiguous.cc
ambiguous.cc:11: error: ISO C++ forbids declaration of ‘Interface’ with no type
ambiguous.cc:23: error: multiple types in one declaration
ambiguous.cc: In member function ‘virtual void MockTest_Call_Foo_X_Test::TestBody()’:
ambiguous.cc:28: error: call of overloaded ‘gmock_function(testing::internal::EqMatcher)’ is ambiguous
ambiguous.cc:19: note: candidates are: testing::internal::MockSpec& Mock::gmock_function(const testing::Matcher&)
ambiguous.cc:20: note:                 testing::internal::MockSpec& Mock::gmock_function(const testing::Matcher&)
ambiguousですよ。ambiguous!

で、どうすっかって。
EXPECT_CALL(m, function(Matcher(Eq(arg)))).WillOnce(Return());
明示的にMatcherで型を示してあげると。

2011年6月21日火曜日

macro trick in googletest

googletestでは、テストケースはこう書くわけ。
#include <gtest/gtest.h>

TEST(SampleTestCase, TestFunc) {
    EXPECT_TRUE(true);
}
C++な割にはスマート。で、なんで、こんなことができるのか。その仕組みを追ってみた。まずは、TEST()マクロ。

include/gtest/gtest.h:
#define TEST(test_case_name, test_name) \
  GTEST_TEST_(test_case_name, test_name, \
              ::testing::Test,\
              ::testing::internal::GetTestTypeId())
ほむ。次はGTEST_TEST_()マクロ。

include/gtest/internal/gtest-internal.h(抜粋):
#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\
class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\
 public:\
  GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\
 private:\
  virtual void TestBody();\
};\
\
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
ほむほむ。GTEST_TEST_CLASS_NAME_()マクロは、test_case_nameとtest_nameを繋げて重複しないクラス名を生成する。上の例だと SampleTestCase_TestFunc_Test となる。最後の行は TestBody() のシグニチャだけ置いて、マクロに続くブロックが TestBody() の実装となるようになっているわけだ。

ちなみに、親クラスとなる ::testing::Test は include/gtest/gtest.h で定義されている。
include/gtest/gtest.h(抜粋):
class Test {
 protected:
  virtual void SetUp();
  virtual void TearDown();
 private:
  virtual void TestBody() = 0;
};
ほむほむほむ。予想どおり。

まとめ

最後に始めに出した例と、展開されたあとのイメージを載せておく。

展開前:
TEST(SampleTestCase, TestFunc) {
    EXPECT_TRUE(true);
}

展開後:
class SampleTestCase_TestFunc_Test : public ::testing::Test {
 public:
  SampleTestCase_TestFunc_Test() {}
 private:
  virtual void TestBody();
};

void SampleTestCase_TestFunc_Test::TestBody() {
    EXPECT_TRUE(true);
}
なるほどねぇ。

2011年6月20日月曜日

Rubyのoptparseでgccみたいなオプションを取る

やりたいことは、これ。
./hoge source -o output
sourceにオプションスイッチが無いのがミソ。

options = {
  :input => $stdin,
  :output => $stdout
}

optp = OptionParser.new
optp.on("-o outputfile", String) {|out_filename|
  options[:output] = File.open(out_filename, 'w')
}

remain_opts = optp.permute(ARGV)
if (remain_opts.size > 1)
  raise "Invalid input count."
elsif (remain_opts.size == 1)
  options[:input] = File.open(remain_opts[0], 'r')
end                                                   
OptionParser#parseでなくて、OptionParser#permuteを使う。