Function pointer link stubs


Last week been test-driving some C code again and found that not much people are familiar with the "function pointer link stub". This is an excellent technique in C to dynamically replace library calls without adjusting the library.

Imagine, you are working on some embedded software. You will have a couple of libraries that you use, but you won't link to it when unit testing. Instead you stub them out. But, a link stub cannot be adjusted on run-time, so how do you handle this? With... "function pointer link stubs".

The idea is simple. You stub out the library and your stub calls a function pointer when its set. When not set, it just returns success.

Here is some example code:

.h (in a library where we cannot change the header)

int aLibraryCallWeCannotChange(int param);

----------------
aLibraryStub.h

extern int (*aLibraryCallWeCannotChangeFp)(int param);

-----------------
aLibraryStub.c

int aLibraryCallWeCannotChange(int param)
{
   if (aLibraryCallWeCannotChangeFp) aLibraryCallWeCannotChangeFp(param);
   return 0;
}

int (*aLibraryCallWeCannotChangeFp)(int param) = NULL;

-------------------------
TestThatUsesLibrary.cpp

TEST_GROUP(blah)
{
   void setup()
   {
       UT_PTR_SET(aLibraryCallWeCannotChangeFp, dynamicStubOfALibraryCall);
   }
}

Now it is easy to place the code of your stub close to your tests and you will never have to change the linker stub code.

Read more

เวลาอู้

เวลาอู้

ผมกำลังอ่านหนังสือ Slack: Getting Past Burnout, Busywork, and the Myth of Total Efficiency ของ Tom DeMarco ซึ่งได้ให้มุมมองใหม่เกี่ยวกับ Slack time หรือเวลาอู้งานกับผม แต่ก่อนจะเล่าว่าผมเห็นอะไร ผมของแบ่งปันมุมมองเวลาผมดูองค์กรก่อนนะ สายการผลิต คนในองค์กรมารวมตั

By Chokchai
สุดยอดทีม (Extraordinary Team)

สุดยอดทีม (Extraordinary Team)

ท้ายหนังสือ Teamwork is an Individual Skill ของ Christopher Avery ได้กล่าวถึงสมการของสุดยอดทีมไว้ดังนี้ครับ Extraordinary Collaboration = Exchange + Expansion + Integrity ผมใช้เวลาอ่านตรงนี้ และอีก 3 บทที่ขยายความเรื่อง Exchange, Expansion และ Integrity อยู่เกือบ 2 สัปดาห์กว่าจะพอเข้าใจมันอย่

By Chokchai
ไล่ตามความฝัน กับ ดูแลตัวเอง

ไล่ตามความฝัน กับ ดูแลตัวเอง

ไล่ตามความฝัน กับ ดูแลตัวเอง ก่อนหน้านี้ผมเคยเล่าถึงขั้วตรงข้าม (Polarity) ระหว่างความคล่องตัวกับความสร้างสรรค์ไปแล้ว ครั้งนี้ผมมองว่า “การไล่ตามความฝัน” และ “การดูแลตัวเอง” (เปรียบเสมือน นักรบ กับ นักรัก) ก็เป็นแสงและเงาของกันและกั

By Chokchai
สกรัมเป็น Empirical process

สกรัมเป็น Empirical process

กระบวนการแก้ปัญหาในโลกแบ่งเป็น 2 แบบ แบบแรกคือ Defined process ซึ่งเป็นกระบวนการที่มีขั้นมีตอนชัดเจน เช่น Waterfall เป็นต้น ส่วนแบบที่สองคือ Empirical process หรือ "กระบวนการเชิงประจักษ์" ซึ่งเป็นการทำไปแล้วก็ปรับไปเรื่อย ๆ สกรัมเป็นแบบหลัง

By Chokchai