Dutch Ephemeris

Free astronomical software

Unit testing binary data

The Dutch Ephemeris uses a binary version of the VSOP data. The main advantage of binary data is the reduced footprint, the resulting binary data is typically three times smaller than the original textual data.

But how to test this?
To check the content of the data you need to have a reader and some interpretation logic. That would mean writing a lot of software before you write a test and that is not what you want.
I decided for a simple approach. After I write the file I compare the result with an existing reference file. Not byte-for-byte but using a checksum. The Adler-32 checksum is good enough for this approach, it is less strict than CRC-32 but it is also faster. And Adler-32 is conventently included in Java in the package java.util.zip.Adler32.

The test goes like this:

A jUnit class DataWriterTest is created to test DataWriter. In @BeforeClass an instance of DataWriter is created which takes care of creating the binary file.

Two additional methods are added to the jUnit class. The first one is
private static byt[] readBytesFromFile(File someFile)
This method returns an array of bytes as read from the file and is pretty much straightfroward.

The second method performs the checksum using the Adler-32 algorithm.

private static int adlerChecksum( byte[] theTextToDigestAsBytes ) {
 Adler32 adler = new Adler32();
 adler.update( theTextToDigestAsBytes );
 return (int) adler.getValue();
}

The test itself

@Test
public void testAdlerChecksum4File() {
  // userDir is defined globally using private static String userDir = System.getProperty("user.dir");
  String referencePath = userDir + File.separator + "files4jUnitArchived" + File.separator + "testdata.bin";
  File referenceFile = new File(referencePath);
  byte[] refBytes = null;
  byte[] actBytes = null;
  try {
     refBytes = readBytesFromFile(referenceFile);
     actBytes = readBytesFromFile(targetFile); // defined in @beforeClass
  } catch (IOException e) {
     fail("IOException :" + e.getMessage());
  }
  int adlerValue4Ref = adlerChecksum(refBytes);
  int actValue4Ref = adlerChecksum(actBytes);
  assertTrue(adlerValue4Ref == actValue4Ref);
}

Chicken or egg?

To have a reference file you need to create one first. I did this using the same software as I am testing with this unit test. Obviously this test does not tell me very much about the correctness of the created file. But it does make it possible to find newly introduced errors which would result in a different binary file. And as soon as the software that uses the binary data is ready I can be sure about its content. And the tests will already be in place.

 

 

Going TDD

Recently I followed a course given by Ron Jeffries and Chet Hendrickson. Together with about 20 colleague’s from Qualogy we learned to use Test Driven Development in a better way. I was already convinced that TDD was the way to go and this course reaffirmed this strongly.

The current code at this site will be rewritten and I will try to use the techniques I learned form Ron and Chet. I will tell you all about my progress – and my errors – in this blog and you are invited to comment on my writings. This site will serve two interests: astronomical software and agile techniques.

What’s next

The Dutch Ephemeris uses binary data files. I wrote a few classes to construct these files from the original data as published for the VSOP87 approach – a method to describe the elements of planetary movements. I dod not yet publish the code for creating these binary files. I will use it for the first attempt to rewrite the code and publish the results, hopefully within a few weeks.

Yes, I know, TDD speeds things up, but I also have my daily job :-)

The Dutch Ephemeris’ very first blog

DutchEphemeris.com is your site for astronomical software. It focuses on celestial mechanics and its first software is a Java implementation of a fast and accurate planetary ephemeris.

The Dutch Ephemeris is an open source library. You can download a package that contains all information, the source and of course the library itself.
The current version 0.9 is in beta. It calculates all planets. At some time in the future the calculation of Sun, Moon and Pluto will be added.

Starting today this site is created with WordPress.