<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        AccessingstaticDataandFunctionsinLegacyC

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 07:39:05
        文檔

        AccessingstaticDataandFunctionsinLegacyC

        AccessingstaticDataandFunctionsinLegacyC:http://www.renaissancesoftware.net/blog/archives/450 Its a new year; last year was a leap year; so the quadrennial reports of leap year bugs are coming in. Apologies are in the p
        推薦度:
        導(dǎo)讀AccessingstaticDataandFunctionsinLegacyC:http://www.renaissancesoftware.net/blog/archives/450 Its a new year; last year was a leap year; so the quadrennial reports of leap year bugs are coming in. Apologies are in the p

        http://www.renaissancesoftware.net/blog/archives/450 It’s a new year; last year was a leap year; so the quadrennial reports of leap year bugs are coming in. Apologies are in the press from Apple, TomTom, and Microsoft. Trains we stopped f

        http://www.renaissancesoftware.net/blog/archives/450

        It’s a new year; last year was a leap year; so the quadrennial reports of leap year bugs are coming in. Apologies are in the press from Apple, TomTom, and Microsoft. Trains we stopped from running in China. Somehow calling them glitches seems to make it someone else’s fault, something out of their control. How long have leap years been around? Julius Caesar introduced Leap Years in the Roman empire over 2000 years ago. The Gregorian calendar has been around since 1682. This is not a new idea, or a new bug.

        I’m going to try to take one excuse away from the programmers that create these bugs by answering a question that comes up all the time, “How do I test static functions in my C code?”

        In code developed using TDD, static functions are tested indirectly through the public interface. As I mentioned in a this article TDD is a code rot radar. It helps you see design problems. Needing direct access to hidden functions and data in C is a sign of code rot. It is time to refactor.

        But what about existing legacy code that has statics? It is probably way past the time for idealism and time for some pragmatism. In this article and the next, we’ll look at how to get your code untouched into the test harness and access those pesky static variables and functions.

        If you don’t mind touching your code, you could change all mentions of static to STATIC. Then using the preprocessor, STATIC can he set to static during production and to nothing for test, making the names globally accessible. In gcc you would use these command line options

      1. For production builds use -dSTATIC=static
      2. For unit test builds use -dSTATIC
      3. Let’s look at two options that, at least for access to statics, you will not have to touch your code to get it under test. First is #include-ing your .c in the test file. In the next article we’ll build a test adapter .c that give access to the hidden parts.

        We are going to revisit code that is similar to the original code responsible for the Zune Bug. I rewrote the code to avoid attracting any lawyers but it is structured similarly to the original Zune driver, complete with static data and functions that must be correct for the function to work.

        The header file provides a data structure and initialization function for figuring out the information about the date. Here is the header:

        typedef struct Date
        {
         int daysSince1980;
         int year;
         int dayOfYear;
         int month;
         int dayOfMonth;
         int dayOfWeek;
        } Date;
         
        void Date_Init(Date *, int daysSince1980);
         
        enum {
         SUN = 0, MON, TUE, WED, THU, FRI, SAT
        };
         
        enum {
         JAN = 0, FEB, MAR, APR, MAY, JUN,
         JUL, AUG, SEP, OCT, NOV, DEC
        };

        Date_Init populates the Date instance passed in. Ignoring the fact that I can probably fully test this by rigging the daysSince1980, and inspecting the contents of the resultingDate structure, we are going to see how we can directly test the hidden functions and data.

        Date_Init has three hidden helper functions.

        void Date_Init(Date* date, int daysSince1980)
        {
         date->daysSince1980 = daysSince1980;
         FirstSetYearAndDayOfYear(date);
         ThenSetMonthAndDayOfMonth(date);
         FinallySetDayOfWeek(date);
        }

        Date_Init is the tip of the iceberg. All the interesting stuff is happening in the hidden data and functions:

        #include "Date.h"
        #include 
         
        enum
        {
         STARTING_YEAR = 1980, STARTING_WEEKDAY = TUE
        };
         
        static const int nonLeapYearDaysPerMonth[12] =
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
         
        static const int leapYearDaysPerMonth[12] =
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
         
        static bool IsLeapYear(int year)
        {
         if (year % 400 == 0)
         return true;
         if (year % 100 == 0)
         return false;
         if (year % 4 == 0)
         return true;
         return false;
        }
         
        static int GetDaysInYear(int year)
        {
         if (IsLeapYear(year))
         return 366;
         else
         return 365;
        }
         
        static void FirstSetYearAndDayOfYear(Date * date)
        {
         int days = date->daysSince1980;
         int year = STARTING_YEAR;
         int daysInYear = GetDaysInYear(year);
         
         while (days > daysInYear)
         {
         year++;
         days -= daysInYear;
         daysInYear = GetDaysInYear(year);
         }
         
         date->dayOfYear = days;
         date->year = year;
        }
         
        static void ThenSetMonthAndDayOfMonth(Date * date)
        {
         int month = 0;
         int days = date->dayOfYear;
         const int * daysPerMonth = nonLeapYearDaysPerMonth;
         if (IsLeapYear(date->year))
         daysPerMonth = leapYearDaysPerMonth;
         
         while (days > daysPerMonth[month])
         {
         days -= daysPerMonth[month];
         month++;
         }
         date->month = month + 1;
         date->dayOfMonth = days;
        }
         
        static void FinallySetDayOfWeek(Date * date)
        {
         date->dayOfWeek =((date->daysSince1980-1)+STARTING_WEEKDAY)%7;
        }
         
        void Date_Init(Date* date, int daysSince1980)
        {
         date->daysSince1980 = daysSince1980;
         FirstSetYearAndDayOfYear(date);
         ThenSetMonthAndDayOfMonth(date);
         FinallySetDayOfWeek(date);
        }

        Let’s say you want to check the days per month vectors. You might want to write a test to check the months against the handy poem we use here in the US: Thirty days, has September, April, June and November; all the rest have thirty-one, except for February. It has 28 except in leap year it has 29.

        If you started by writing this test…

        TEST(Date, sept_has_30_days)
        {
         LONGS_EQUAL(30, nonLeapYearDaysPerMonth[SEP]);
        }

        … you get this error:

        DateTest.cpp:41: error: 'nonLeapYearDaysPerMonth' was not declared in this scope

        Let’s get access to the hidden statics in the test case by including Date.c instead ofDate.h in DateTest.cpp. The full test case file looks like this now:

        #include "CppUTest/TestHarness.h"
         
        extern "C"
        {
        #include "Date.c"
        }
         
        TEST_GROUP(Date)
        {
        };
         
        TEST(Date, sept_has_30_days)
        {
         LONGS_EQUAL(30, nonLeapYearDaysPerMonth[SEP]);
        }

        With a little refactoring days per month vectors can be checked like this:

        #include "CppUTest/TestHarness.h"
         
        extern "C"
        {
        #include "Date.c"
        }
         
        TEST_GROUP(Date)
        {
         const int * daysPerYearVector;
         
         void setup()
         {
         daysPerYearVector = nonLeapYearDaysPerMonth;
         }
         
         void itsLeapYear()
         {
         daysPerYearVector = leapYearDaysPerMonth;
         }
         
         void CheckNumberOfDaysPerMonth(int month, int days)
         {
         LONGS_EQUAL(days, daysPerYearVector[month]);
         }
         
         void ThirtyDaysHasSeptEtc()
         {
         CheckNumberOfDaysPerMonth(SEP, 30);
         CheckNumberOfDaysPerMonth(APR, 30);
         CheckNumberOfDaysPerMonth(JUN, 30);
         CheckNumberOfDaysPerMonth(NOV, 30);
         
         CheckNumberOfDaysPerMonth(OCT, 31);
         CheckNumberOfDaysPerMonth(DEC, 31);
         CheckNumberOfDaysPerMonth(JAN, 31);
         CheckNumberOfDaysPerMonth(MAR, 31);
         CheckNumberOfDaysPerMonth(MAY, 31);
         CheckNumberOfDaysPerMonth(JUL, 31);
         CheckNumberOfDaysPerMonth(AUG, 31);
         }
         
         void ExceptFebruaryHas(int days)
         {
         CheckNumberOfDaysPerMonth(FEB, days);
         }
        };
         
        TEST(Date, non_leap_year_day_per_month_table)
        {
         ThirtyDaysHasSeptEtc();
         ExceptFebruaryHas(28);
        }
         
        TEST(Date, leap_year_day_per_month_table)
        {
         itsLeapYear();
         ThirtyDaysHasSeptEtc();
         ExceptFebruaryHas(28);
        }

        You have access to all the hidden stuff, so you can write the test for the static functions:

        IsLeapYear(), GetDaysInYear(), FirstSetYearAndDayOfYear(),ThenSetMonthAndDayOfMonth(), and FinallySetDayOfWeek().

        If Date been an abstract data type, with its data structure hidden in the .c file, the tests would all have access to structure members hidden from the rest of the world.

        There is a downside to this approach, which probably does not matter in this case, but could in others. You can only have one test file that can include a given .c file. In the next article we’ll solve that problem.

        Have you heard of any interesting leap year bugs? Did you prevent your own?

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        AccessingstaticDataandFunctionsinLegacyC

        AccessingstaticDataandFunctionsinLegacyC:http://www.renaissancesoftware.net/blog/archives/450 Its a new year; last year was a leap year; so the quadrennial reports of leap year bugs are coming in. Apologies are in the p
        推薦度:
        標(biāo)簽: in and data
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 激情五月亚洲色图| 亚洲AV无码专区亚洲AV伊甸园| 激情综合亚洲色婷婷五月| 91精品免费观看| 亚洲午夜精品国产电影在线观看| 国产成人精品免费久久久久| 亚洲6080yy久久无码产自国产| 最近免费中文字幕大全视频 | 国内永久免费crm系统z在线| 在线亚洲精品福利网址导航| 中文字幕免费观看全部电影| 久久亚洲国产欧洲精品一| 91大神免费观看| 77777亚洲午夜久久多喷| 天天干在线免费视频| 国产亚洲精品第一综合| 亚洲日本中文字幕一区二区三区| 久久精品国产亚洲精品2020| 9277手机在线视频观看免费| 亚洲 欧洲 日韩 综合在线| 午夜时刻免费入口| 一级毛片免费毛片毛片| 亚洲国产精品va在线播放| 8x8x华人永久免费视频| 一本色道久久综合亚洲精品蜜桃冫| 日本特黄特色aa大片免费| 全黄A免费一级毛片| 久久久久亚洲Av无码专| 岛国片在线免费观看| 亚欧国产一级在线免费| 亚洲美女视频一区| 免费永久在线观看黄网站| 国产免费看JIZZ视频| 免费看黄网站在线看| 亚洲国产高清视频| 啦啦啦www免费视频| 你懂的在线免费观看| 亚洲AV无码一区二区三区在线| 国产免费一区二区三区VR| 免费成人在线视频观看| 2020久久精品亚洲热综合一本|