Saturday, July 30, 2016

Inheritance in Lua

Consider this code:

This is the output when run:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/oop$ ./inheritance.lua
Calling constructor for table: 0x2414180
Accessing key = new for table: 0x2412f40; self = table: 0x2414180
Calling constructor for table: 0x2412f40
Accessing key = deposit for table: 0x2411eb0; self = table: 0x2412f40
Accessing key = deposit for table: 0x2412f40; self = table: 0x2414180
Account:deposit(): self = table: 0x2411eb0
Accessing key = balance for table: 0x2411eb0; self = table: 0x2412f40
Accessing key = balance for table: 0x2412f40; self = table: 0x2414180
Accessing key = withdraw for table: 0x2411eb0; self = table: 0x2412f40
SpecialAccount:withdraw(): self = table: 0x2411eb0
Accessing key = getLimit for table: 0x2411eb0; self = table: 0x2412f40
SpecialAccount:getLimit(): self = table: 0x2411eb0
Accessing key = get_balance for table: 0x2411eb0; self = table: 0x2412f40
Accessing key = get_balance for table: 0x2412f40; self = table: 0x2414180
Account:get_balance(): self = table: 0x2411eb0
-100
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/oop$

Notice how searching for a key is recursive. When a key is not found in s, it is searched in SpecialAccount. When the key is not found in SpecialAccount, it is searched in Account.

Classes in Lua

This is an adaptation of the code found here:

When run, the output looks like this:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/oop$ ./l.lua 
Calling constructor for table: 0x8a2160
Accessing key = deposit for table: 0x8a1df0; self = table: 0x8a2160
Account:deposit(): self = table: 0x8a1df0
Accessing key = get_balance for table: 0x8a1df0; self = table: 0x8a2160
Account:get_balance(): self = table: 0x8a1df0
a's balance: 100
Calling constructor for table: 0x8a2160
Accessing key = deposit for table: 0x89a4a0; self = table: 0x8a2160
Account:deposit(): self = table: 0x89a4a0
Accessing key = balance for table: 0x89a4a0; self = table: 0x8a2160
Account:balance = 0
Accessing key = withdraw for table: 0x89a4a0; self = table: 0x8a2160
Account:withdraw(): self = table: 0x89a4a0
Accessing key = get_balance for table: 0x89a4a0; self = table: 0x8a2160
Account:get_balance(): self = table: 0x89a4a0
b's balance: 60
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/oop$ 



Thursday, July 21, 2016

Compiling and running Google test and Google mock in Ubuntu

To compile and run Google Test and Google Mock in Ubuntu, you can follow these steps.

First download Google Test from here. Extract the downloaded zip file to a directory of your choice.

After extraction, you will see a folder named googletest-master. Enter this folder.

Inside the googletest-master folder, you will see these two folders: googletest and googlemock.

To compile and test run google test, go to the googletest directory. There you will see a make directory. Go inside the make directory and type 'make'.

Executing the make command will create the executable file, sample1_unittest. You can then run this executable file:

kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ pwd; ls
/home/kuyu/dkuyu/bin/googletest-master/googletest/make
Makefile
kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ make && ./sample1_unittest
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest-all.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating gtest_main.a
a - gtest-all.o
a - gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 6 tests.
kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ 

You can follow similar steps to compile and test run Google Mock in Ubuntu. From the googletest-master directory, go to the googlemock/make directory. Then type make. The executable created by the make command is named gmock_test.

You can actually study the Makefile file in the googletest/make and googlemock/make directories. There you can edit the GTEST_DIR, USER_DIR, and GMOCK_DIR (for googlemock).

Wednesday, July 20, 2016

Google test

Getting started in Google test is also difficult.

To setup Google test to run in Ubuntu, I followed this site.

CppUMock example

This is an example CppUMock test. It consists of 3 files: test_main.cpp, test.cpp and makefile.

test_main.cpp:

test.cpp:

makefile:

Output:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/cpp/cpputest/ho$ make && ./a.out
g++ -g -I/usr/local/include  -c test_main.cpp
g++ -g -I/usr/local/include  -c test.cpp
g++ -g -o a.out test.o test_main.o -L/usr/local/lib -lCppUTest -lCppUTestExt
testBody: pProtocolFsm=0x189d4c0
ProtocolProcedure: Got 3
ProtocolProcedure: Got 5
ProtocolProcedure: Got 7
.
OK (1 tests, 1 ran, 3 checks, 0 ignored, 0 filtered out, 1 ms)

kuyu@castor-ub:~/dkuyu/Dropbox/practice/cpp/cpputest/ho$ 

Wednesday, July 13, 2016

cpputest

I spent several hours figuring out how to run cpputest in Ubuntu. I searched several websites, but they were not helpful. You can imagine the agony that I went through.

Finally, I came across this stackoverflow question. Thank you, pradeep (the author of the question (and also the answer)).

Well, first you need to install CppUTest in Ubuntu:

% sudo apt-get install cpputest

After that, create the files: test.cpp, test_main.cpp, and makefile

For the makefile file, be sure to use tab indentations and not spaces.

After creating the files:

kuyu@castor-ub:~/dkuyu/practice/cpp/cpputest/hello$ make 
g++ -g -I/usr/local/include  -c test_main.cpp
g++ -g -I/usr/local/include  -c test.cpp
g++ -g -o mytest test.o test_main.o -L/usr/local/lib -lCppUTest -lCppUTestExt
kuyu@castor-ub:~/dkuyu/practice/cpp/cpputest/hello$ ./mytest 

test.cpp:15: error: Failure in TEST(FirstTestGroup, SecondTest)
expected <hello>
but was  <world>
difference starts at position 0 at: <          world     >
                                              ^

.
test.cpp:10: error: Failure in TEST(FirstTestGroup, FirstTest)
Fail me!

.
Errors (2 failures, 2 tests, 2 ran, 1 checks, 0 ignored, 0 filtered out, 1 ms)

kuyu@castor-ub:~/dkuyu/practice/cpp/cpputest/hello$ 

It finally ran.

Saturday, July 9, 2016

Iterator swap for vector proxy class

Here is an example of iterator swapping for the vector<bool> proxy class:

Here is the output:

kuyu@castor-ub:~/dkuyu/practice/cpp/iter_swap_bool$ g++ -Wformat=0 iter_swap_bool.cpp && ./a.out
Before swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=1
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=0
After 1st swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=0
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=1
After 2nd swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=1
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=0
kuyu@castor-ub:~/dkuyu/practice/cpp/iter_swap_bool$

Some questions to consider:
  1. What happens if the function starting at line 25 is deleted?
  2. What happens if the function at line 54 returned proxy& instead of only proxy?

iterator_traits

Been trying to read this book:

Abraham, David; Gurtovoy, Aleksey. 2004. C++ template metaprogramming: Concepts, tools, and techniques from Boost and beyond. Addison Wesley.

Tried to code a little on iterator_traits:

When I run it:

kuyu@castor-ub:~/dkuyu/practice/cpp/iterator_traits$ g++ iterator_traits.cpp && ./a.out 
Before:
a = a
b = b
After:
a = b
b = a
kuyu@castor-ub:~/dkuyu/practice/cpp/iterator_traits$ 

Example boost:fusion

Example of boost::fusion vector. It is like a C++ tuple.

I guess you can guess what the output is:

kuyu@castor-ub:~/dkuyu/practice/cpp/boost/fusion$ g++ -I/home/kuyu/dkuyu/bin/boost_1_60_0 vec.cpp && ./a.out 
howdy
kuyu@castor-ub:~/dkuyu/practice/cpp/boost/fusion$

Tuesday, July 5, 2016

make

I've always been confused about make and makefiles.

One of the simplest tutorials on make I found is this.