Page 1 of 1

Automated tests

Posted: 17 Jan 2019 17:49
by njn
I think OpenTTD would benefit from some unit tests. Have there been any efforts to introduce automated testing to the codebase? I'll try and put something together with googletest (https://github.com/google/googletest).

Re: Automated tests

Posted: 18 Jan 2019 09:55
by planetmaker
njn wrote:I think OpenTTD would benefit from some unit tests. Have there been any efforts to introduce automated testing to the codebase? I'll try and put something together with googletest (https://github.com/google/googletest).
It might. But it's not easy to actually create/ run any unit tests on OpenTTD. But of course: be our guest :)

Re: Automated tests

Posted: 21 Feb 2019 01:52
by njn
Alright, well in case anyone is interested... I've got a branch going for this here: https://github.com/OpenTTD/OpenTTD/pull/7254

I tried adding some tests for the ReallyAdjustBrightness() function in the 32bpp sse blitter, but I ran into some trouble compiling this object file in a standalone way, because it uses the _screen and _palette externs. This is possibly solvable, though. Anyways, any function with concrete inputs and outputs is a good candidate for unit testing.

Re: Automated tests

Posted: 21 Feb 2019 14:26
by Eddi
njn wrote:because it uses the _screen and _palette externs.
that's what i mean when i say it's hard to introduce unit tests when the program is already done, and not designed with units in mind. you get all theese interdependency issues that makes it difficult to isolate sections to test

Re: Automated tests

Posted: 21 Feb 2019 15:32
by jfs
njn wrote:I tried adding some tests for the ReallyAdjustBrightness() function in the 32bpp sse blitter, but I ran into some trouble compiling this object file in a standalone way, because it uses the _screen and _palette externs. This is possibly solvable, though. Anyways, any function with concrete inputs and outputs is a good candidate for unit testing.
This is where you will have to introduce some mocking. The palette and screen definitions seem to be pretty straight forward and can probably be defined as static (non-const) data in the test program.

Re: Automated tests

Posted: 21 Feb 2019 16:14
by njn
Eddi wrote:
njn wrote:because it uses the _screen and _palette externs.
that's what i mean when i say it's hard to introduce unit tests when the program is already done, and not designed with units in mind. you get all theese interdependency issues that makes it difficult to isolate sections to test
These problems are very solvable.. you can either:
* Make the code more loosely coupled
* Mock out the interfaces that you need to just get things basically working for testing purposes, as jfs points out above. That's probably the route I'll take here, and will give me a chance to try out https://github.com/google/googletest/tr ... googlemock

Re: Automated tests

Posted: 21 Feb 2019 17:35
by Eddi
njn wrote:These problems are very solvable..
yes, but it's an awful lot of grunt work

Re: Automated tests

Posted: 22 Feb 2019 13:28
by njn
Eddi wrote:
njn wrote:These problems are very solvable..
yes, but it's an awful lot of grunt work
True.. it can definitely be seen that way. idk why I find it kinda fun / interesting. Maybe it's a personal problem >_<

I guess I like finding overlooked cases that aren't behaving correctly. In fact I already found one: the GreatestCommonDivisor() function can return negative integers when one of its inputs is negative. These should always be positive. This probably doesn't have any effect on the code, since I think it's only using this function with positive integers. But still, GreatestCommonDivisor() gladly accepts negative integers as input, so it might as well be correct.

Re: Automated tests

Posted: 22 Feb 2019 13:40
by planetmaker
njn wrote:
Eddi wrote:
njn wrote:These problems are very solvable..
yes, but it's an awful lot of grunt work
True.. it can definitely be seen that way. idk why I find it kinda fun / interesting. Maybe it's a personal problem >_<
In science we have a saying: one person's noise is another person's data. Similar applies to what people consider fun work. Fortunately :)