⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptypes_test.cxx

📁 PTypes是一个扩充了多线程和网络功能的STL库
💻 CXX
📖 第 1 页 / 共 3 页
字号:
/* * *  C++ Portable Types Library (PTypes) *  Version 2.0.2  Released 17-May-2004 * *  Copyright (C) 2001-2004 Hovik Melikyan * *  http://www.melikyan.com/ptypes/ * */#include <stdlib.h>#include <stdio.h>#include "pport.h"#include "ptypes.h"#include "pstreams.h"#include "pinet.h"#include "ptime.h"#ifndef PTYPES_ST#include "pasync.h"#endifUSING_PTYPESvoid passert(bool cond){    if (!cond)    {        fatal(0xa3, "*** ASSERTION FAILED ***");    }}void showstr(const char* shouldbe, const char* is) {    passert(strcmp(shouldbe, is) == 0);    pout.putf("[%s] %s\n", shouldbe, is);}void showstr(const char* shouldbe, const string& is) {    showstr(shouldbe, pconst(is));}void showhex(const char* shouldbe, const char* is, int islen) {    string s;    int i;    for (i = 0; i < islen; i++)        s += itostring((unsigned char)is[i], 16, 2);    s = lowercase(s);    showstr(shouldbe, s);}void showint(int shouldbe, int is) {    passert(shouldbe == is);    pout.putf("[%d] %d\n", shouldbe, is);}void showint(large shouldbe, large is) {    passert(shouldbe == is);    pout.putf("[%lld] %lld\n", shouldbe, is);}void showchar(char shouldbe, char is) {    passert(shouldbe == is);    pout.putf("[%c] %c\n", shouldbe, is);}//// string test//void const_string_call(const string& s){    showchar('R', s[2]);}void string_test1() {    pout.put("\n--- STRING CLASS\n");    static char strbuf[10] = "STRING";    char c = 'A';    string s1 = "string";    s1 = s1;    s1 += s1;    del(s1, 6, 6);    string s2 = s1;    string s3;    string s4(strbuf, strlen(strbuf));    string s5 = 'A';    string s6 = c;    showstr("string", s1);    showstr(s1, s2);    showstr("", s3);    showstr("STRING", s4);    const_string_call(s4);    showchar('I', s4[3]);    showint(6, length(s4));    showint(2, refcount(s1));    clear(s2);    showint(1, refcount(s1));    s2 = s1;    unique(s1);    showint(1, refcount(s1));    setlength(s1, 64);    string s7 = s1;    setlength(s1, 3);    showstr("str", s1);    showstr("strING", s1 += copy(s4, 3, 3));    del(s1, 3, 3);    showstr("str", s1);    ins("ing", s1, 0);    showstr("ingstr", s1);    s2 = "str" + s1 + "ing";    showstr("stringstring", s2);    s3 = s2;    s3 = "strungstrung";    s3 = s2;    s3 = "strung";    s2 = "str" + s1;    s2 = s1 + "str";    s2 += "str";    s2 = 's' + s1;    s2 = s1 + 's';    s2 += 's';        s2 = c + s1;    s2 = s1 + c;    s2 += c;    s2 = 'a';    s2 = c;}void string_test2() {    pout.put("\n--- STRING CLASS\n");    string s1 = "ingstr";    string s2 = "stringstring";    string s4 = "STRING";    showint(1, pos('t', s2));    showint(2, pos("ri", s2));    showint(3, pos(s1, s2));    showint(1, contains("tr", s2, 1));    showint(0, contains("tr", s2, 2));    showint(1, s4 == "STRING");    showint(1, "STRING" == s4);    showchar('R', s4[2]);    string s5 = 'A';    showint(1, s5 == 'A');    showint(1, 'A' == s5);    showstr("123456789", itostring(123456789));    showstr("123", itostring(char(123)));    showstr("-000123", itostring(-123, 10, 7, '0'));    showstr("0ABCDE", itostring(0xabcde, 16, 6));    showstr("-9223372036854775808", itostring(LARGE_MIN));    showstr("18446744073709551615", itostring(ULARGE_MAX));    showint(1234, (int)stringtoi("1234"));    showint(large(0x15AF), stringtoue("15AF", 16));    showint(LARGE_MAX, stringtoue("5zzzzzzzzzz", 64));    try    {        // out of range by 1        stringtoue("18446744073709551616", 10);        fatal(0xb0, "Conversion overflow not detected");    }    catch (econv* e)    {        showstr("Out of range: '18446744073709551616'", e->get_message());        delete e;    }    showint(large(123), stringtoie("123"));    showint(large(-123), stringtoie("-123"));    showint(LARGE_MIN, stringtoie("-9223372036854775808"));    showint(LARGE_MAX, stringtoie("9223372036854775807"));    try    {        // out of range by 1        stringtoie("9223372036854775808");        fatal(0xb0, "Conversion overflow not detected");    }    catch (econv* e)    {        showstr("Out of range: '9223372036854775808'", e->get_message());        delete e;    }    showstr("abcabc", lowercase(s1 = "aBCAbc"));    showstr("abcabc", lowercase(s1));}void string_benchmarks(){    pout.put("\n--- STRING BENCHMARKS\n");    int i;    string s1 = "string";    string s2;    // for the first time we run the test to let the VM settle down and stop swapping    for (i = 0; i < 156000; i++)        s2 += s1;    // here is the actual test    clear(s2);    datetime start = now();    for (i = 0; i < 156000; i++)        s2 += s1;    datetime diff = now() - start;    pout.putf("Performance index compared to WinNT on P3/800MHz (smaller = better):\n%.3f\n", diff / 1000.0);}//// cset test//void cset_test() {    pout.put("\n--- CSET CLASS\n");    cset s1 = "~09~0a~0d~F0 A-Z~~";    cset s2 = "A-F";    char c = 'B';    showstr("~09~0a~0d A-Z~~~f0", asstring(s1));    s1 -= char(0xf0);    s1 -= cset("~00-~20");    showstr("A-Z~~", asstring(s1));    s1 -= s2;    showstr("G-Z~~", asstring(s1));    s1 += s2;    s1 += ' ';    showstr(" A-Z~~", asstring(s1));    showint(1, s2 == cset("A-F"));    showint(1, s2 <= s1);    s1 -= 'A';    s1 -= c;    showint(0, s2 <= s1);    s1 = s1 + char(0xf1);    showint(1, char(0xf1) & s1);    showint(0, char(0xf2) & s1);}//// podlist//void const_podlist_test(const tpodlist<int, true>& p){//    int& i = p[0];    showint(7, p[1]);}void podlist_test(){    pout.put("\n--- PODLIST\n");    tpodlist<int, true> p;    p.add() = 6;    p.add(8);    p.ins(1, 7);    showint(7, p[1]);    const_podlist_test(p);    showint(3, p.get_count());    p.set_count(5);    p.ins(5) = 10;    showint(10, p.top());    p.pop();    tpodlist<int, true> p1;    p1.add(p);    p1.pop();    p1.add(p);}//// ptrlist//struct known: public unknown{    int value;    known(int ivalue): value(ivalue) {}};typedef tobjlist<known> knownlist;string ol_asstring(const knownlist& s){    string ret = "{";    for (int i = 0; i < s.get_count(); i++) {        if (i > 0)            concat(ret, ", ");        ret += itostring(s[i]->value);    }    concat(ret, '}');    return ret;}void ptrlist_test(){    pout.put("\n--- PTRLIST\n");    knownlist s1(true);    known* obj;    s1.add(new known(10));    s1.ins(0, new known(5));    s1.ins(2, obj = new known(20));    s1.add(new known(30));    s1.add(new known(40));    s1.put(4, new known(45));    s1.del(4);    s1.del(1);    s1[0];    showint(3, s1.get_count());    showint(1, s1.indexof(obj));    showstr("{5, 20, 30}", ol_asstring(s1));    s1.clear();    showstr("{}", ol_asstring(s1));}//// strlist//typedef tstrlist<known> knownstrlist;string sl_asstring(const knownstrlist& s){    string ret = "{";    for (int i = 0; i < s.get_count(); i++)    {        if (i > 0)            concat(ret, ", ");        ret += s.getkey(i) + ":" + itostring(s[i]->value);    }    concat(ret, '}');    return ret;}void strlist_test(){    pout.put("\n--- STRLIST\n");    knownstrlist s1(SL_OWNOBJECTS);    known* obj;    s1.add("ten", new known(10));    s1.ins(0, "five", new known(5));    s1.ins(2, "twenty", obj = new known(20));    s1.add("thirty", new known(30));    s1.add("forty", new known(40));    s1.put(4, "forty five", new known(45));    s1.del(4);    s1.del(1);    showint(3, s1.get_count());    showint(1, s1.indexof(obj));    showint(2, s1.indexof("thirty"));    showint(2, s1.indexof("THIRTY"));    showint(-1, s1.indexof("forty"));    showstr("{five:5, twenty:20, thirty:30}", sl_asstring(s1));    knownstrlist s2(SL_OWNOBJECTS | SL_SORTED | SL_CASESENS);    s2.add("five", new known(5));    s2.add("ten", new known(10));    s2.add("twenty", new known(20));    s2.add("thirty", new known(30));    s2.add("forty", new known(40));    showint(5, s2.get_count());    showint(3, s2.indexof("thirty"));    showint(-1, s2.indexof("THIRTY"));    showint(-1, s2.indexof("hovik"));    showstr("{five:5, forty:40, ten:10, thirty:30, twenty:20}", sl_asstring(s2));    s2.clear();    showstr("{}", sl_asstring(s2));    tstrlist<known> s3(SL_OWNOBJECTS | SL_SORTED | SL_DUPLICATES);    s3.add("a", nil);    s3.add("b", nil);    s3.add("b", nil);    s3.add("b", nil);    s3.add("b", nil);    s3.add("b", nil);    s3.add("b", nil);    s3.add("c", nil);    showint(1, s3.indexof("b"));    s3.del(1, 2);    tstrlist<known> s(SL_OWNOBJECTS | SL_SORTED);    s.put("five", new known(5));    s.put("ten", new known(10));    s.put("twenty", new known(20));    s.put("thirty", new known(30));    s.put("forty", new known(40));    showint(20, s["twenty"]->value);    showint(0, pintptr(s["hovik"]));    showint(5, s.get_count());    s.put("twenty", nil);    showint(4, s.get_count());}//// textmap//void textmap_test(){    pout.put("\n--- TEXTMAP CLASS\n");    textmap c;    c.put("name1", "value1");    c.put("name2", "value2");    c.put("name1", "value3");    showstr("name2", c.getkey(1));    c.put("name2", "");    showint(1, c.get_count());    showstr("value3", c["name1"]);    showstr("", c["name2"]);}//// streams//char buf1[] = "This is a test.";char buf2[] = "The file should contain readable human text. :)";const char* fname = "stmtest.txt";void outfile_test() {    pout.put("\n--- OUTFILE CLASS\n");    outfile f(fname, false);    f.set_umode(0600);    f.set_bufsize(3);    f.open();    f.put(buf1[0]);    f.put(buf1[1]);    f.put("is is a TEST.");    f.seek(-5, IO_END);    f.put("tes*/");    f.seek(13);    f.put("t.");    f.puteol();    f.close();    f.set_append(true);    f.open();    f.write(buf2, strlen(buf2));    f.puteol();    f.close();    pnull.put("This should go to nowhere I");    pnull.put("This should go to nowhere II");}void infile_test() {    pout.put("\n--- INFILE CLASS\n");    compref<instm> f = &pin;    f = new infile(fname);    f->set_bufsize(3);    char temp[4];    f->open();    pout.putf("%c", f->get());    pout.putf("%s\n", pconst(f->line()));    f->read(temp, sizeof temp - 1);    temp[sizeof temp - 1] = 0;    pout.putf("%s", temp);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -