Example #1
0
bool torch_data() {
    using namespace torch;
    std::string respath = Path(projectdir).Append("/tests/res").GetPath();
    std::string testpath = Path(respath).Append("test.go").GetPath();
    
//    torch::Data d = torch::File::GetBytes(testpath);
//    InfoLog("%s\n", d.ToString().c_str());
    
    Data buf;
    
    int count = 0;
    
    // Fill
    buf.Fill(0, 1);
    buf.Fill(1, 2, 0);
    buf.DumpHex();
    buf.Free();
    
    // Alloc
    
    buf.Alloc(sizeof("hello,world"));
    if ( buf.GetSize() != 12 ) return false;
    buf.Free();
    if ( buf.GetSize() != 0 ) return false;
    printf("[%d] %zu\n", ++count, buf.GetSize());
    
    char *ptr = "hello,world";
    size_t size = (strlen(ptr)+1) * sizeof(char);
    buf.Alloc( size );
    
    // CopyFrom
    
    buf.CopyFrom(ptr, size);
    ((char *)buf.GetBytes())[5] = '|';
    printf("print string: %s\n", buf.ToString().c_str());

    // Insert
    echo("[Insert] Free");
    buf.Free();
    buf.DumpHex();
    
    buf.Free();
    echo ("[Insert] Insert \"\\1\\2\\3\\4\\5\\6\\7\" -> 0xff(3)");
    char s1[] = "\1\2\3\4\5\6\7";
    buf.Insert(0, s1, sizeof(s1));
    buf.DumpHex();
    buf.DumpBinary();
    buf.DumpBinary(1);
    buf.DumpBinary(2);

    if (buf.ToHex() != std::string("0102030405060700")) return false;
    if (buf.ToBinary(1) != std::string("00000001 00000010 00000011 00000100 00000101 00000110 00000111 00000000")) return false;
    if (buf.ToBinary(2) != std::string("0000000100000010 0000001100000100 0000010100000110 0000011100000000")) return false;
    if (buf.ToBinary() != std::string("0000000100000010000000110000010000000101000001100000011100000000")) return false;

    
    {
        Data b = buf;
        b.DumpHex();
        b.Insert(-1, 0xff);
        b.DumpHex();
        if (b.ToHex() != "01020304050607FF00") return false;
    }
    {
        char str[] = "hello,world";
        Data b;
        b.Insert(0, str, sizeof(str));
        printf("%s\n", b.GetBytes());
    }
    
    buf.Insert(3, 0xff);
    buf.DumpHex();
    if (buf.ToHex() != "010203FF0405060700") return false;

    buf.Free();
    char ss[] = "hello,world";
    buf.Insert(0, ss, sizeof(ss));
    printf("%s\n", buf.GetBytes());
    if (std::string("hello,world") != (char*)buf.GetBytes()) return false;
    buf.Insert(3, '|');
    printf("%s\n", buf.GetBytes());
    if (std::string("hel|lo,world") != (char*)buf.GetBytes()) return false;

    // Append
    echo ("[Append] Append \\0");
    buf.Free();
    buf.Append('\0');
    if (buf.ToHex() != "00") return false;
    buf.DumpHex();
    echo ("[Append] Append \"\\1\\2\\3\\4\\5\\6\\7\"");
    buf.Append(s1, sizeof(s1));
    if (buf.ToHex() != "000102030405060700") return false;
    buf.DumpHex();
    Data b;
    b.Append((void*)"\0\0", 2);
    buf.Append(b);
    if (buf.ToHex(2) != "0001 0203 0405 0607 0000 00") return false;
    buf.DumpHex(2);

    // Erase
    buf.Free().Append(s1, sizeof(s1));
    buf.DumpHex();
    echo ("[Erase] Erase byte");
    buf.Erase(buf.GetSize()-1, 1);
    if (buf.ToHex(2) != "0102 0304 0506 07") return false;
    buf.DumpHex(2);
    
    echo ("[Erase] Erase first byte");
    buf.Erase(0, 1);
    if (buf.ToHex(2) != "0203 0405 0607") return false;
    buf.DumpHex(2);
    
    echo ("[Erase] Erase mid byets...");
    buf.Erase(3, 10);
    if (buf.ToHex(2) != "0203 04") return false;
    buf.DumpHex(2);
    
    echo ("[Erase] Erase all...");
    buf.Erase(0, 100);
    if (buf.ToHex() != "") return false;
    buf.DumpHex();

    // Replace
    buf.Free();
    echo ("[Replace]");
    buf.Insert(0, s1, sizeof(s1));
    buf.DumpHex();
    echo ("[Replace] 0x00(0-1)");
    buf.Replace(0, 1, 0x00);
    if (buf.ToHex(2) != "0002 0304 0506 0700") return false;
    buf.DumpHex(2);
    
    echo ("[Replace] 0x00(4-2)");
    buf.Replace(4, 2, 0x00);
    if (buf.ToHex(2) != "0002 0304 0007 00") return false;
    buf.DumpHex(2);
    
    echo ("[Replace] 0x00(4-100)");
    buf.Replace(4, 100, 0x00);
    if (buf.ToHex(2) != "0002 0304 00") return false;
    buf.DumpHex(2);
    
    echo ("[Replace] 0x00(0-100)");
    buf.Replace(0, 100, 0x00);
    if (buf.ToHex(2) != "00") return false;
    buf.DumpHex(2);
    
    {
        Data d = buf;
        echo ("[Replace] 0x00(0-100)");
        d.Replace(-1, 1, 0x11);
        if (d.ToHex() != "11") return false;
        d.DumpHex();
    }

    // GetByte
    echo ("[GetBytes]");
    char s3[] = "hello,world,luwei";
    buf.Insert(0, s3, sizeof(s3)-1);
    buf.DumpHex();
#define TEST_BUFFER_PRINT_1(__CH, __CH2) \
printf("%02X => %c | %s\n", __CH, __CH, (__CH2==__CH)?"TRUE":"FALSE"); \
if (__CH2!=__CH) return false
    
    TEST_BUFFER_PRINT_1(buf.GetByte(0), 'h');
    TEST_BUFFER_PRINT_1(buf.GetByte(1), 'e');
    TEST_BUFFER_PRINT_1(buf.GetByte(2), 'l');
    TEST_BUFFER_PRINT_1(buf.GetByte(-1), '\0');
    TEST_BUFFER_PRINT_1(buf.GetByte(-2), 'i');
    TEST_BUFFER_PRINT_1(buf.GetByte(-3), 'e');
    TEST_BUFFER_PRINT_1(buf.GetByte(10), 'd');
    TEST_BUFFER_PRINT_1(buf.GetByte(-10), 'r');
    
    // Find
    buf.Free();
    char s4[] = "hello,world,luwei";
    buf.Insert(0, s4, sizeof(s4));
    printf("[Find] %s\n", buf.GetBytes());
    
#define TEST_BUFFER_00(__ID, __CH, __INDEX, __D) \
int c##__ID = buf.IndexOf(__CH, __INDEX); \
printf("[Find] %d\n", c##__ID); \
if (c##__ID != __D) return false
    
    // Find-char
    TEST_BUFFER_00(0, 'i', -2, 16);
    TEST_BUFFER_00(1, 'o', 5, 7);
    TEST_BUFFER_00(2, 'o', 4, 4);
    TEST_BUFFER_00(3, 'o', -(int)buf.GetSize(), 4);
    TEST_BUFFER_00(4, '\0', -1, 17);
    TEST_BUFFER_00(5, '\0', 0, 17);
    TEST_BUFFER_00(6, '\0', (int)buf.GetSize()-1, 17);
    
    // FindLast
    //    char s4[] = "hello,world,luwei";
#define TEST_BUFFER_01(__ID, __CH, __INDEX, __D) \
int c##__ID = buf.LastIndexOf(__CH, __INDEX); \
printf("[FindLast] %d\n", c##__ID); \
if (c##__ID != __D) return false
    
    TEST_BUFFER_01(7, 'i', -2, 16);
    TEST_BUFFER_01(8, 'o', 5, 4);
    TEST_BUFFER_01(9, 'o', -(int)buf.GetSize(), -1);
    TEST_BUFFER_01(10, '\0', -1, 17);
    TEST_BUFFER_01(11, '\0', 0, -1);
    TEST_BUFFER_01(12, '\0', (int)buf.GetSize()-1, 17);
    
    return true;
}