Dalvik vm Hash – Unit Test

來源:互聯網
上載者:User
Dalvik 虛擬機器 雜湊表 之 單元測試

 

在dvm源碼
dalvik/vm/Test/TestHash.cpp 是dvm內建的UT測試案例。其實現很直觀,我們簡單的分析一下:

 

測試案例:

所有的測試都在 dvmTestHash()函數裡進行。測試流程如下.

 

 

第一輪是基本API測試,包括 雜湊表建立、插入單元,搜尋單元,遍曆單元以及釋放雜湊表。

首次建立測試的雜湊表:

    LOGV("TestHash BEGIN");

    //rambo: allocate 12 but insert more, to let hash to extend
    pTab = dvmHashTableCreate(dvmHashSize(12), free);
    if (pTab == NULL)
        return false;

 

接著往該表裡加入雜湊單元,單元的值是 字串“entry xx” xx是從1 到 kNumTestEntries整數,單元的鍵是用函數dvmComputeUtf8Hash計算出對應的值。

    dvmHashTableLock(pTab);

    /* add some entries */
    for (i = 0; i < kNumTestEntries; i++) {
        sprintf(tmpStr, "entry %d", i);
        hash = dvmComputeUtf8Hash(tmpStr);
        dvmHashTableLookup(pTab, hash, strdup(tmpStr),
            (HashCompareFunc) strcmp, true);
    }

    dvmHashTableUnlock(pTab);

 

然後測試剛加入的所有的單元都可以搜尋到:

 

    /* make sure we can find all entries */
    for (i = 0; i < kNumTestEntries; i++) {
        sprintf(tmpStr, "entry %d", i);
        hash = dvmComputeUtf8Hash(tmpStr);
        str = (const char*) dvmHashTableLookup(pTab, hash, tmpStr,
                (HashCompareFunc) strcmp, false);
        if (str == NULL) {
            LOGE("TestHash: failure: could not find '%s'", tmpStr);
            /* return false */
        }
    }

 

然後嘗試搜尋一個不存在的單元:

    /* make sure it behaves correctly when entry not found and !doAdd */
    sprintf(tmpStr, "entry %d", 17);
    hash = dvmComputeUtf8Hash(tmpStr);
    str = (const char*) dvmHashTableLookup(pTab, hash, tmpStr,
            (HashCompareFunc) strcmp, false);
    if (str == NULL) {
        /* good */
    } else {
        LOGE("TestHash found nonexistent string (improper add?)");
    }

 

調用dumpForeach(pTab);測試 dvmHashForeach函數,它的實現裡

static void dumpForeach(HashTable* pTab)
{
    int count = 0;

    //printf("Print from foreach:\n");
    dvmHashForeach(pTab, printFunc, &count);
    if (count != kNumTestEntries) {
        LOGE("TestHash foreach test failed");
        assert(false);
    }
}

傳入的遍曆執行函數是printFunc。它遍曆真箇雜湊表,並簡單的增加單元數:

static int printFunc(void* data, void* arg)
{
    //printf("  '%s'\n", (const char*) data);
    // (should verify strings)

    int* count = (int*) arg;
    (*count)++;
    return 0;
}

dumpForeach用來測試,經過dvmHashForeach遍曆後,單元數目是否正確。

 

接著調用dumpIterator(pTab);來檢測雜湊表遍曆器是否工作正常。其實現也是就簡單的遍曆左右單元,並檢查單元數目是否正確。

static void dumpIterator(HashTable* pTab)
{
    int count = 0;

    //printf("Print from iterator:\n");
    HashIter iter;
    for (dvmHashIterBegin(pTab, &iter); !dvmHashIterDone(&iter);
        dvmHashIterNext(&iter))
    {
        //const char* str = (const char*) dvmHashIterData(&iter);
        //printf("  '%s'\n", str);
        // (should verify strings)
        count++;
    }
    if (count != kNumTestEntries) {
        LOGE("TestHash iterator test failed");
        assert(false);
    }
}

 

最後調用dvmHashTableFree釋放掉雜湊表,第一輪測試結束。

    /* make sure they all get freed */
    dvmHashTableFree(pTab)

 

第二輪測試用來檢驗“非常規”情景:

首先也是建立雜湊表,其初始容量只有2個單元:

    pTab = dvmHashTableCreate(dvmHashSize(2), free);
    if (pTab == NULL)
        return false;

 

然後,嘗試插入兩個單元,其hash值一樣!

    hash = 0;

    /* two entries, same hash, different values */
    const char* str1;
    str1 = (char*) dvmHashTableLookup(pTab, hash, strdup("one"),
            (HashCompareFunc) strcmp, true);
    assert(str1 != NULL);
    str = (const char*) dvmHashTableLookup(pTab, hash, strdup("two"),
            (HashCompareFunc) strcmp, true);

 

然後,移除第一個單元:

    /* remove the first one */
    if (!dvmHashTableRemove(pTab, hash, (void*)str1))
        LOGE("TestHash failed to delete item");
    else
        free((void*)str1);     // "Remove" doesn't call the free func

 

檢驗雜湊表裡是否只剩一個單元了:

    /* make sure iterator doesn't included deleted entries */
    int count = 0;
    HashIter iter;
    for (dvmHashIterBegin(pTab, &iter); !dvmHashIterDone(&iter);
        dvmHashIterNext(&iter))
    {
        count++;
    }
    if (count != 1) {
        LOGE("TestHash wrong number of entries (%d)", count);
    }

然後檢查剛移除的單元無法再搜尋到,第二個單元卻可以:

    /* see if we can find them */
    str = (const char*) dvmHashTableLookup(pTab, hash, (void*)"one",
            (HashCompareFunc) strcmp,false);
    if (str != NULL)
        LOGE("TestHash deleted entry has returned!");
    str = (const char*) dvmHashTableLookup(pTab, hash, (void*)"two",
            (HashCompareFunc) strcmp,false);
    if (str == NULL)
        LOGE("TestHash entry vanished");

 

接著檢查,往該雜湊表裡插入17個單元,以檢測雜湊表自動擴容沒有問題:

    /* force a table realloc to exercise tombstone removal */
    for (i = 0; i < 20; i++) {
        sprintf(tmpStr, "entry %d", i);
        str = (const char*) dvmHashTableLookup(pTab, hash, strdup(tmpStr),
                (HashCompareFunc) strcmp, true);
        assert(str != NULL);
    }

最後釋放掉 雜湊表,測試結束

    dvmHashTableFree(pTab);
    LOGV("TestHash END");

    return true

 

測試執行

hash UT 可以在dvm初始化時執行,前提是開啟debug開關:

 

// dalvik/vm/init.cpp

std::string dvmStartup(int argc, const char* const argv[],
        bool ignoreUnrecognized, JNIEnv* pEnv)
{

...

 

#ifndef NDEBUG
    //rambo: perform quick UT
    if (!dvmTestHash())
        LOGE("dvmTestHash FAILED");

...
#endif

...

}

 

要開啟debug開關,我們可以在編譯dvm是指定

 

// dalvik/vm/dvm.mk

# Make a debugging version when building the simulator (if not told
# otherwise) and when explicitly asked.
dvm_make_debug_vm := false
ifneq ($(strip $(DEBUG_DALVIK_VM)),)
  dvm_make_debug_vm := $(DEBUG_DALVIK_VM)
endif

ifeq ($(dvm_make_debug_vm),true)

...

 LOCAL_CFLAGS += -UNDEBUG -DDEBUG=1 -DLOG_NDEBUG=1 -DWITH_DALVIK_ASSERT

else

...

endif  # !dvm_make_debug_vm

 

關於dvm編譯指令碼,請參考
Dalvik
vm make file config and source tree 一文。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.