Mac dylib rpath framework bundle explanation

The purpose of this article is to teach you how to properly configure a dylib / framework on the mac and the application linking against it. If you don’t do this properly, you could be in for a long time wasting ordeal. In this article we will describe, in enough detail, how the linking process works in order to get things working and hopefully teach you enough to configure things on your own if you decide to do something in a non-standard way. We will also give you the tools and knowledge required in order to debug issues related to library loading, particularly with rpaths.

The subject
When you load an application, the dynamic linker (it’s actually an application on the mac called dyld I believe), will look at all of the references (symbols) in the application and try to resolve them (perhaps on load, perhaps not, who knows these days). In order to do this, it needs to know what libraries this application depends upon, where the libraries are on the hard drive, and also, what libraries those libraries depend upon so it can load them too, recursively. If the dynamic linker can’t find these libraries, it can’t load them, bottom line. You’ll end up with an error such as “bundle is corrupt, invalid, or missing resources” or some error like that.

Read the rest of this entry »

Testing at Microsoft

I was a contract employee at Microsoft, an SDET I. While I was there I learned quite a lot about testing, and I really believe it made me a much better programmer. Although my time there was short lived, I managed to retain some of my testing knowledge, and I hope to share that with you now. So I’ll quickly and briefly cover the basics of testing. I’m not talking about unit testing either. I’ll break down the types of tests you have into different categories. There may be more.

  • BVT (Base verification tests)
  • –Positive test cases
  • FVT (Functional verification tests)
  • –Positive Test Cases (Tester tests for expected failures )
  • –Negative Test Cases (We catch unexpected failures during testing (hopefully) )

BVT (Base Verification Tests)
These are the basic tests performed to show that the operation is basically working. Sometimes you just want to run the BVT tests because they’re faster and shorter. Example:

It is short and to the point. TEST is a simple macro that checks if the return value of the function is equal to the second parameter. It checks to see if 2*2/1 == 5. Nothing more. It is fast, but it doesn’t check everything. This test is also called a positive test case. A positive test case consists of passing in valid data, and expecting to get a valid result.

FVT (Functional Verification Tests)
The FVTs are a much more in depth testing stage. It consists of a much more thorough testing of the functions, in both quantity, and quality. FVTs consists of the BVT tests as well. So they may be duplicated, but this isn’t necessary. In some cases, the FVTs consists of modifications to the BVTs.
From the example above:

As you can see, we now have a lot more test cases and they’re broken up into positive and negative test cases. We may want then to be in separate functions too, but I didn’t do that here. Our positive test cases have a lot more cases and are simple modifications to the BVT.

The FVT also consists of our negative cases, where, if the function fails, (gives an invalid result the user can’t use) it needs to fail in an expected and deterministic way. Under no circumstances should it crash. When it crashes, this is an “unexpected failure,” and is usually caught very soon, since your tests will also crash. This is the most common unexpected failure. I can’t really think of any other right now.

Also, all of the negative test cases will fail in this scenario because nothing in the function is being thrown. And our tests are expecting it to throw the correct exceptions. When testing, it’s important to test common boundary values. In the example above, good things to test are 0, -1, 1, INF, -INF, NaNs, and other such things. When writing code, it’s important to check for the same boundary conditions immediately at the top of the function.

One final note before I leave. These are NOT unit tests. Unit tests sometimes overlap the BVTs, but they are written by the developer who wrote the code (in this case the Calc function). So they are not written by the tester (or SDET).

Math trickery for 1D & 2D arrays

Once upon a time, there was a language that didn’t have 2 dimensional arrays. This greatly upset the programmer. Fortunately, the programmer was swift in the art of arithmetic and overcame this obstacle easily.

Lets say for example, we only have access to a 1 dimensional array data structure and it has 50 elements in it.

Lets break this array up into 10 rows and 5 columns to make a grid. So how do we get access to a position say data[4][3]?
There are two things I’m going to teach you now:
1) Retrieve the column & row numbers from a linear index counter.
2) Retrieve the index from a row & column numbers.

Before I continue, i want you to notice that the row and column numbers are zero based. So they start at zero, not one.

Lets do #1 first:

Read the rest of this entry »

C++ Casting

I created a table that has lots of useful information about c++ casts.

dynamic_cast
  1. Only can be used with pointers and references.
  2. Throws bad_cast if reference can’t be cast, returns null if pointer can’t be cast.
  3. Performs runtime checking.
  4. Makes sure cast is valid & results in a complete object.
  5. Down casts are not valid (base to derived) UNLESS base is polymorphic, gives compilation error.
  6. Up casts are ALWAYS valid (casting to base).
  7. Compilers must have RTTI turned on.
  8. Can cast any pointer type to void*.
static_cast
  1. Does not perform runtime checking. (compile time only)
  2. Makes sure objects are related using hierarchy tree.
  3. Up & down casts are valid.
reinterpret_cast
  1. Just copies the address over with pointers.
  2. No checking at all.
  3. Same as C-Style.
const_cast
  1. Used to remove or set constness.

How to get a perfectly sized client area

I’ve been fighting for a while trying to figure out how to make the size of a window’s client area, exactly a specified size. There is some information out there on the internet, but it’s not that great. Even the MSDN documentation is a little confusing. So i set out on a quest.

Lets say you want your window client size to be exactly 800 x 600. This means 800 x 600 of viewable space and does not include scroll bars or menu bars. Here is how I accomplished it. I know it is correct because I measured it with a paint program.

For this little snippet, I have made the first two elements the top left corner of the window positioned on the screen. Not the client area position, but the window overall. The second two are the width and height in pixels of the client area. Now lets adjust our window and create it.

Read the rest of this entry »

Graphics Pipeline

This article is simply a reference for the standard graphics pipeline. Sometimes I forget what happens in what space.
I know it’s pretty crappy, maybe I’ll redo it one day to make it nice and neat.

Intersection and Non-intersection routines

More than once I have been asked to find the solution to a simple problem related to finding the intersection of two axis aligned boxes. Also more than once I’ve stumbled during the interview even though I knew the problem was easy. What was the problem? Why was I getting confused? Attempting to figure out why, I sat down and thoroughly solved the problem and figured out why I was having issues. It turns out that I was getting confused about whether I was solving the problem looking for the intersection of the two boxes, or looking for the non-intersection of two boxes. I’ll explore both and why you would use one or the other.

What is the difference? It is basically the difference between asking these two questions:
Are the two boxes intersecting?
Are the two boxes separated (not intersecting)?

Read the rest of this entry »

Composition vs Inheritance

After performing quite a bit of research on casting, I decided I could post a table, or list of some important things that I found out regarding composition vs. inheritance.

Changing implementation of either technique does not break clients code. Separation of interface and implementation is the key.
Inheritance
  1. Can use polymorphism and dynamic binding
  2. Old code works with new code
  3. Base class is fragile (the interface)
  4. Allows derived class code to reuse base class code
  5. “Is-a” relationship
Composition
  1. Derived = front-end, base = backend, client code still works now.
  2. Can change backend without affecting client.
  3. Can swap objects out at runtime.
  4. Works by delegation, a change is required, but not necessarily to the derived class’s interface which keeps client’s code working.
  5. Use if all you want is code reuse.

Read the rest of this entry »

What is endianness?

What does it mean for the byte order to be little-endian versus big-endian? Basically, it’s how the computer interprets multi-byte values. For example, we know that a single byte can take on the values of 0-254 inclusive. The arrangement of the bits within that single byte is pretty standard.

(1)2^7 + (0)2^6 + (0)2^5 + (0)2^4 + (1)2^3 + (0)2^2 + (0)2^1 + (1)2^0 = 137

Looking at the above and only looking at the ones and zeroes we have 10001001 = 137 in decimal.
We usually have the location of the smallest bit (least significant bit) on the right going to the largest bit (most significant bit) on the left. So this is for a single byte. What about multi-byte values?

First lets discuss some memory addressing issues. When you have a pointer that points to a memory address: char* p = ……; Something like that (we are assuming a char is a single byte), then p points to a single byte.

If we increment it like this: ++p, then p will point to the next byte in memory. It’s important to note that addresses in memory are the location of a single byte. You can never address bits. Although by using bit shift operators and such, you can manipulate the bits within that single byte.

Let’s expand on this a little bit. Lets say we have an array of characters:
charletters[]={‘a’,’b’,’c’,’d’};

Read the rest of this entry »