tdbTest.cpp 1.2 KB
Newer Older
H
Hongze Cheng 已提交
1 2
#include "gtest/gtest.h"

H
Hongze Cheng 已提交
3
#include "tdbInt.h"
H
more  
Hongze Cheng 已提交
4

H
Hongze Cheng 已提交
5
TEST(tdb_test, simple_test) {
H
Hongze Cheng 已提交
6 7
  int    ret;
  STEnv *pEnv;
H
Hongze Cheng 已提交
8
  STDb  *pDb;
H
Hongze Cheng 已提交
9
  int    nData = 2000000;
H
more  
Hongze Cheng 已提交
10

H
Hongze Cheng 已提交
11
  // Open Env
H
Hongze Cheng 已提交
12
  ret = tdbEnvOpen("tdb", 4096, 25600, &pEnv);
H
Hongze Cheng 已提交
13
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
14

H
Hongze Cheng 已提交
15
  // Create a database
H
more  
Hongze Cheng 已提交
16
  ret = tdbDbOpen("db.db", TDB_VARIANT_LEN, TDB_VARIANT_LEN, NULL, pEnv, &pDb);
H
Hongze Cheng 已提交
17
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19 20 21
  {
    char  key[64];
    char  val[64];
H
Hongze Cheng 已提交
22
    void *pVal = NULL;
H
Hongze Cheng 已提交
23
    int   vLen;
H
Hongze Cheng 已提交
24

H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    {  // Insert some data

      for (int i = 1; i <= nData; i++) {
        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);
        ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val));
        GTEST_ASSERT_EQ(ret, 0);
      }
    }

    {  // Query the data
      for (int i = 1; i <= nData; i++) {
        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);

H
Hongze Cheng 已提交
40
        ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen);
H
Hongze Cheng 已提交
41 42 43 44 45
        GTEST_ASSERT_EQ(ret, 0);

        GTEST_ASSERT_EQ(vLen, strlen(val));
        GTEST_ASSERT_EQ(memcmp(val, pVal, vLen), 0);
      }
H
Hongze Cheng 已提交
46
    }
H
Hongze Cheng 已提交
47 48 49

    {  // Loop to query the data
    }
H
Hongze Cheng 已提交
50
  }
H
Hongze Cheng 已提交
51 52

  ret = tdbDbDrop(pDb);
H
Hongze Cheng 已提交
53
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
54

H
Hongze Cheng 已提交
55 56
  // Close a database
  tdbDbClose(pDb);
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58 59 60
  // Close Env
  ret = tdbEnvClose(pEnv);
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
61
}