コード例 #1
0
K recieve_data(I x)
{
    static char buf[BUFFER_SIZE];
    
    read_bytes(sizeof(int), &buf);
    int tnamesize = 0;
    memcpy(&tnamesize, buf, sizeof(int));
   
    read_bytes(tnamesize, &buf);
    buf[tnamesize] = '\0';
    
    K tname = ks(buf);

    read_bytes(sizeof(J), &buf);
    J size = 0;
    memcpy(&size, buf, sizeof(J));

    read_bytes(size, &buf);
    K bytes = ktn(KG, size);
    memcpy(kG(bytes), &buf, (size_t) size);

    K result = k(0, ".u.upd", tname, d9(bytes), (K) 0);
    r0(bytes);

    if (result != 0) {
        r0(result);
    }

    return (K) 0;
}
コード例 #2
0
ファイル: Wind_subscribe.cpp プロジェクト: emptist/q_Wind
		// Data processor (executed within q's main thread)
		K invokeCallback(I socket) {
			static_assert(sizeof(::SOCKET) == sizeof(I), "SOCKET vs I: type mismatch!");
			::SOCKET sock = socket;
			assert(sock != INVALID_SOCKET);

			// Receive (WQID, len, serialized_K) from subscription thread
#			define RECV_CHECK(expectedSize, errorMsg)	\
			if (recvd != (expectedSize)) {	\
				std::cerr << "<recv> " << (errorMsg) << ": " << recvd << " < " << (expectedSize) << std::endl;	\
				return K_NIL;	\
			}
			::WQID qid = 0;
			int recvd = ::recv(sock, reinterpret_cast<char*>(&qid), sizeof(::WQID), 0);
			RECV_CHECK(sizeof(::WQID), "WQID incomplete");
			std::size_t len = 0;
			recvd = ::recv(sock, reinterpret_cast<char*>(&len), sizeof(len), 0);
			RECV_CHECK(sizeof(len), "size incomplete");
			if (len > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
				std::cerr << "<recv> serialized data (" << len << ") > 2G" << std::endl;
				return K_NIL;
			}
			q::K_ptr serialized(ktn(KB, len));
			std::memset(kG(serialized.get()), 0, len);
			recvd = ::recv(sock, reinterpret_cast<char*>(kG(serialized.get())), len, 0);
			RECV_CHECK(len, "data incomplete");
#			undef RECV_CHECK

			// Deserialize K object
#			if KX_USE_OKX
			//@ref https://groups.google.com/forum/#!topic/personal-kdbplus/pjsugT7590A
			if (!okx(serialized.get())) {
				std::cerr << "<recv> bad data: ["
					<< util::hexBytes(kG(serialized.get()), static_cast<std::size_t>(serialized->n))
					<< ']' << std::endl;
				return K_NIL;
			}
#			endif
			q::K_ptr result(d9(serialized.get()));

			// Identify the origial query and callback
			std::string const callback = REGISTRY[qid];
			if (callback.empty()) {
				std::cerr << "unknown WQID: " << qid << std::endl;
				return K_NIL;
			}

			static_assert(std::is_same<::WQID, J>::value, "WQID data type mismatch");
			q::K_ptr output(k(0, const_cast<S>(callback.c_str()), kj(qid), result.release(), K_NIL));
			if (output->t == -128) {
				std::cerr << "<q> '" << output->s << std::endl;
			}
			return output.release();
		}
コード例 #3
0
ファイル: Pattern_random.cpp プロジェクト: jborme/dpps
void dpps::Pattern_random::generate () {
    // We have do declare them all. We cannot do a switch of if,
    // because distributions would get out of scope.
    // The only other possibility would be to place the switch inside
    // the loop, but it would be very inefficient to call the constructor
    // everytime.
    // So we have to pay for overhead and initialize them all in te beginning.
    // It's probably a negligible amount of time and memory as compared to the
    // loop.

    // signification of parametres depends on the engine. Some engines
    // need only one parametre.
    // For all user-changeable values (p1 and p2), 1.0 would have been the
    // default if we had not specified it, so it is an acceptable value for the
    // user as well.
    const double p1 {pattern_settings. p1} ;
    const double p2 {pattern_settings. p2} ;

    std::uniform_real_distribution<double> d0 (-p1, p1) ; // min, max
    // Normal-type
    std::normal_distribution<double> d1 (0.0, p1) ;    // average = 0, sigma
    std::lognormal_distribution<double> d2 (0.0, p1) ; // average = 0, m
    std::chi_squared_distribution<double> d3 (p1) ;    // n
    std::cauchy_distribution<double> d4 (p1, p2) ;     // a, b
    std::fisher_f_distribution<double> d5 (p1, p2) ;   // m, n
    std::student_t_distribution<double> d6 (p1) ;      // n
    // Poisson-type
    std::exponential_distribution<double> d7 (p1) ;    // lambda
    std::gamma_distribution<double> d8 (p1, p2) ;      // alpha, beta
    std::weibull_distribution<double> d9 (p1, p2) ;    // a, b

    Polyline p ;
    p. closed = true ;
    double x {0.0}, y {0.0} ;
    long_unsigned_int i {0}, attempts {0} ;
    //const double s {pattern_settings. side / 2.0} ;
    if (pattern_settings. max_attempts < pattern_settings. number)
        pattern_settings. max_attempts =
            std::numeric_limits<long_unsigned_int>::max () ;
    //std::cout << "hello" << i << " " << pattern_settings. number << " " <<  attempts << " " << pattern_settings. max_attempts << std::endl ;
    while ((i < pattern_settings. number) &&
           (attempts < pattern_settings. max_attempts)) {
        bool overlap = false ;
        switch (pattern_settings. type) {
            case type_uniform_real_distribution: // 0
                x = d0 (pseudorandom_generator) ;
                y = d0 (pseudorandom_generator) ;
                //std::cout << "alea 0 : " << x << " " << y << std::endl ;
                break ;
            case type_normal_distribution: // 1
                x = d1 (pseudorandom_generator) ;
                y = d1 (pseudorandom_generator) ;
                break ;
            case type_lognormal_distribution: // 2
                x = d2 (pseudorandom_generator) ;
                y = d2 (pseudorandom_generator) ;
                break ;
            case type_chi_squared_distribution: // 3
                x = d3 (pseudorandom_generator) ;
                y = d3 (pseudorandom_generator) ;
                break ;
            case type_cauchy_distribution: // 4
                x = d4 (pseudorandom_generator) ;
                y = d4 (pseudorandom_generator) ;
                break ;
            case type_fisher_f_distribution: // 5
                x = d5 (pseudorandom_generator) ;
                y = d5 (pseudorandom_generator) ;
                break ;
            case type_student_t_distribution: // 6
                x = d6 (pseudorandom_generator) ;
                y = d6 (pseudorandom_generator) ;
                break ;
            case type_exponential_distribution: // 7
                x = d7 (pseudorandom_generator) ;
                y = d7 (pseudorandom_generator) ;
                break ;
            case type_gamma_distribution: // 8
                x = d8 (pseudorandom_generator) ;
                y = d8 (pseudorandom_generator) ;
                break ;
            case type_weibull_distribution: // 9
                x = d9 (pseudorandom_generator) ;
                y = d9 (pseudorandom_generator) ;
                break ;
            default:
                break ;
        }
        if (pattern_settings. avoid_overlap) {
            for (long_unsigned_int j = 0 ; j < polylines. size () ; j++) {
                if ((polylines[j]. vertices[0] - Vertex(x, y)).norm2_square() <
                    pattern_settings. diametre*pattern_settings. diametre) {
                    overlap = true ;
                    break ;
                 }
            }
        }

        bool within_limits =
            (((fabs (x - pattern_settings. x0) < pattern_settings. lx / 2.0) ||
             (pattern_settings. lx <= 0)) &&
            ((fabs (y - pattern_settings. y0) < pattern_settings. ly / 2.0) ||
             (pattern_settings. ly <= 0))) ;

        // Either we are inside lx, or user set it to negative to disable it.
        if  (within_limits && !overlap) {
            p. push_back (Vertex (x, y)) ;
            p. dose = pattern_settings. diametre ;
            polylines. push_back (p) ;
            p. vertices. clear () ; // does not change that p. closed == true ;
            i++ ;
        }
        attempts++ ;
    }
}
コード例 #4
0
ファイル: fs.t.cpp プロジェクト: austinwagner/task
int main (int, char**)
{
  UnitTest t (110);

  // Ensure environment has no influence.
  unsetenv ("TASKDATA");
  unsetenv ("TASKRC");

  // Path ();
  Path p0;
  t.is (p0._data, "", "Path::Path");

  // Path (const Path&);
  Path p1 = Path ("foo");
  t.is (p1._data, Directory::cwd () + "/foo", "Path::operator=");

  // Path (const std::string&);
  Path p2 ("~");
  t.ok (p2._data != "~", "~ expanded to " + p2._data);

  Path p3 ("/tmp");
  t.ok (p3._data == "/tmp", "/tmp -> /tmp");

  // operator==
  t.notok (p2 == p3, "p2 != p3");

  // Path& operator= (const Path&);
  Path p3_copy (p3);
  t.is (p3._data, p3_copy._data, "Path::Path (Path&)");

  // operator (std::string) const;
  t.is ((std::string) p3, "/tmp", "Path::operator (std::string) const");

  // std::string name () const;
  Path p4 ("/a/b/c/file.ext");
  t.is (p4.name (), "file.ext",  "/a/b/c/file.ext name is file.ext");

  // std::string parent () const;
  t.is (p4.parent (), "/a/b/c",  "/a/b/c/file.ext parent is /a/b/c");

  // std::string extension () const;
  t.is (p4.extension (), "ext",  "/a/b/c/file.ext extension is ext");

  // bool exists () const;
  t.ok (p2.exists (), "~ exists");
  t.ok (p3.exists (), "/tmp exists");

  // bool is_directory () const;
  t.ok (p2.is_directory (), "~ is_directory");
  t.ok (p3.is_directory (), "/tmp is_directory");

  // bool is_link () const;
  t.notok (p2.is_link (), "~ !is_link");

  // bool readable () const;
  t.ok (p2.readable (), "~ readable");
  t.ok (p3.readable (), "/tmp readable");

  // bool writable () const;
  t.ok (p2.writable (), "~ writable");
  t.ok (p3.writable (), "/tmp writable");

  // bool executable () const;
  t.ok (p2.executable (), "~ executable");
  t.ok (p3.executable (), "/tmp executable");

  // static std::string expand (const std::string&);
  t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~");
  t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/");

  // static std::vector <std::string> glob (const std::string&);
  std::vector <std::string> out = Path::glob ("/tmp");
  t.ok (out.size () == 1, "/tmp -> 1 result");
  t.is (out[0], "/tmp", "/tmp -> /tmp");

  out = Path::glob ("/t?p");
  t.ok (out.size () == 1, "/t?p -> 1 result");
  t.is (out[0], "/tmp", "/t?p -> /tmp");

  out = Path::glob ("/[s-u]mp");
  t.ok (out.size () == 1, "/[s-u]mp -> 1 result");
  t.is (out[0], "/tmp", "/[s-u]mp -> /tmp");

  // bool is_absolute () const;
  t.notok (p0.is_absolute (), "'' !is_absolute");
  t.ok    (p1.is_absolute (), "foo is_absolute");
  t.ok    (p2.is_absolute (), "~ is_absolute (after expansion)");
  t.ok    (p3.is_absolute (), "/tmp is_absolute");
  t.ok    (p4.is_absolute (), "/a/b/c/file.ext is_absolute");

  Directory tmp ("tmp");
  tmp.create ();
  t.ok (tmp.exists (), "tmp dir created.");

  File::write ("tmp/file.t.txt", "This is a test\n");
  File f6 ("tmp/file.t.txt");
  t.ok (f6.size () == 15, "File::size tmp/file.t.txt good");
  t.ok (f6.mode () & S_IRUSR, "File::mode tmp/file.t.txt good");
  t.ok (File::remove ("tmp/file.t.txt"), "File::remove tmp/file.t.txt good");

  // operator (std::string) const;
  t.is ((std::string) f6, Directory::cwd () + "/tmp/file.t.txt", "File::operator (std::string) const");

  t.ok (File::create ("tmp/file.t.create"), "File::create tmp/file.t.create good");
  t.ok (File::remove ("tmp/file.t.create"), "File::remove tmp/file.t.create good");

  // basename (std::string) const;
  t.is (f6.name (), "file.t.txt", "File::basename tmp/file.t.txt --> file.t.txt");

  // dirname (std::string) const;
  t.is (f6.parent (), Directory::cwd () + "/tmp", "File::dirname tmp/file.t.txt --> tmp");

  // bool rename (const std::string&);
  File f7 ("tmp/file.t.2.txt");
  f7.append ("something\n");
  f7.close ();

  t.ok (f7.rename ("tmp/file.t.3.txt"),  "File::rename did not fail");
  t.is (f7._data, Directory::cwd () + "/tmp/file.t.3.txt",    "File::rename stored new name");
  t.ok (f7.exists (),                    "File::rename new file exists");
  t.ok (f7.remove (),                    "File::remove tmp/file.t.3.txt good");
  t.notok (f7.exists (),                 "File::remove new file no longer exists");

  // Test permissions.
  File f8 ("tmp/file.t.perm.txt");
  f8.create (0744);
  t.ok (f8.exists (),                    "File::create perm file exists");
  mode_t m = f8.mode ();
  t.ok    (m & S_IFREG,                  "File::mode tmp/file.t.perm.txt S_IFREG good");
  t.ok    (m & S_IRUSR,                  "File::mode tmp/file.t.perm.txt r-------- good");
  t.ok    (m & S_IWUSR,                  "File::mode tmp/file.t.perm.txt -w------- good");
  t.ok    (m & S_IXUSR,                  "File::mode tmp/file.t.perm.txt --x------ good");
  t.ok    (m & S_IRGRP,                  "File::mode tmp/file.t.perm.txt ---r----- good");
  t.notok (m & S_IWGRP,                  "File::mode tmp/file.t.perm.txt ----w---- good");
  t.notok (m & S_IXGRP,                  "File::mode tmp/file.t.perm.txt -----x--- good");
  t.ok    (m & S_IROTH,                  "File::mode tmp/file.t.perm.txt ------r-- good");
  t.notok (m & S_IWOTH,                  "File::mode tmp/file.t.perm.txt -------w- good");
  t.notok (m & S_IXOTH,                  "File::mode tmp/file.t.perm.txt --------x good");
  f8.remove ();
  t.notok (f8.exists (),                 "File::remove perm file no longer exists");

  tmp.remove ();
  t.notok (tmp.exists (),                "tmp dir removed.");
  tmp.create ();
  t.ok (tmp.exists (), "tmp dir created.");

  // Directory (const File&);
  // Directory (const Path&);
  Directory d0 (Path ("tmp"));
  Directory d1 (File ("tmp"));
  Directory d2 (File (Path ("tmp")));
  t.is (d0._data, d1._data, "Directory(std::string) == Directory (File&)");
  t.is (d0._data, d2._data, "Directory(std::string) == Directory (File (Path &))");
  t.is (d1._data, d2._data, "Directory(File&)) == Directory (File (Path &))");

  // Directory (const Directory&);
  Directory d3 (d2);
  t.is (d3._data, Directory::cwd () + "/tmp", "Directory (Directory&)");

  // Directory (const std::string&);
  Directory d4 ("tmp/test_directory");

  // Directory& operator= (const Directory&);
  Directory d5 = d4;
  t.is (d5._data, Directory::cwd () + "/tmp/test_directory", "Directory::operator=");

  // operator (std::string) const;
  t.is ((std::string) d3, Directory::cwd () + "/tmp", "Directory::operator (std::string) const");

  // virtual bool create ();
  t.ok (d5.create (), "Directory::create tmp/test_directory");
  t.ok (d5.exists (), "Directory::exists tmp/test_directory");

  Directory d6 (d5._data + "/dir");
  t.ok (d6.create (), "Directory::create tmp/test_directory/dir");

  File::create (d5._data + "/f0");
  File::create (d6._data + "/f1");

  // std::vector <std::string> list ();
  std::vector <std::string> files = d5.list ();
  std::sort (files.begin (), files.end ());
  t.is ((int)files.size (), 2, "Directory::list 1 file");
  t.is (files[0], Directory::cwd () + "/tmp/test_directory/dir", "file[0] is tmp/test_directory/dir");
  t.is (files[1], Directory::cwd () + "/tmp/test_directory/f0", "file[1] is tmp/test_directory/f0");

  // std::vector <std::string> listRecursive ();
  files = d5.listRecursive ();
  std::sort (files.begin (), files.end ());
  t.is ((int)files.size (), 2, "Directory::list 1 file");
  t.is (files[0], Directory::cwd () + "/tmp/test_directory/dir/f1", "file is tmp/test_directory/dir/f1");
  t.is (files[1], Directory::cwd () + "/tmp/test_directory/f0", "file is tmp/test_directory/f0");

  // virtual bool remove ();
  t.ok (File::remove (d5._data + "/f0"), "File::remove tmp/test_directory/f0");
  t.ok (File::remove (d6._data + "/f1"), "File::remove tmp/test_directory/dir/f1");

  t.ok (d6.remove (), "Directory::remove tmp/test_directory/dir");
  t.notok (d6.exists (), "Directory::exists tmp/test_directory/dir - no");

  t.ok (d5.remove (), "Directory::remove tmp/test_directory");
  t.notok (d5.exists (), "Directory::exists tmp/test_directory - no");

  // bool remove (const std::string&);
  Directory d7 ("tmp/to_be_removed");
  t.ok (d7.create (), "Directory::create tmp/to_be_removed");
  File::create (d7._data + "/f0");
  Directory d8 (d7._data + "/another");
  t.ok (d8.create (), "Directory::create tmp/to_be_removed/another");
  File::create (d8._data + "/f1");
  t.ok (d7.remove (), "Directory::remove tmp/to_be_removed");
  t.notok (d7.exists (), "Directory tmp/to_be_removed gone");

  // static std::string cwd ();
  std::string cwd = Directory::cwd ();
  t.ok (cwd.length () > 0, "Directory::cwd returned a value");

  // bool parent (std::string&) const;
  Directory d9 ("/one/two/three/four.txt");
  t.ok (d9.up (),                   "parent /one/two/three/four.txt --> true");
  t.is (d9._data, "/one/two/three", "parent /one/two/three/four.txt --> /one/two/three");
  t.ok (d9.up (),                   "parent /one/two/three --> true");
  t.is (d9._data, "/one/two",       "parent /one/two/three --> /one/two");
  t.ok (d9.up (),                   "parent /one/two --> true");
  t.is (d9._data, "/one",           "parent /one/two --> /one");
  t.ok (d9.up (),                   "parent /one --> true");
  t.is (d9._data, "/",              "parent /one --> /");
  t.notok (d9.up (),                "parent / --> false");

  // Test permissions.
  umask (0022);
  Directory d10 ("tmp/dir.perm");
  d10.create (0750);
  t.ok (d10.exists (),               "Directory::create perm file exists");
  m = d10.mode ();
  t.ok    (m & S_IFDIR,             "Directory::mode tmp/dir.perm S_IFDIR good");
  t.ok    (m & S_IRUSR,             "Directory::mode tmp/dir.perm r-------- good");
  t.ok    (m & S_IWUSR,             "Directory::mode tmp/dir.perm -w------- good");
  t.ok    (m & S_IXUSR,             "Directory::mode tmp/dir.perm --x------ good");
  t.ok    (m & S_IRGRP,             "Directory::mode tmp/dir.perm ---r----- good");
  t.notok (m & S_IWGRP,             "Directory::mode tmp/dir.perm ----w---- good");
  t.ok    (m & S_IXGRP,             "Directory::mode tmp/dir.perm -----x--- good");
  t.notok (m & S_IROTH,             "Directory::mode tmp/dir.perm ------r-- good");
  t.notok (m & S_IWOTH,             "Directory::mode tmp/dir.perm -------w- good");
  t.notok (m & S_IXOTH,             "Directory::mode tmp/dir.perm --------x good");
  d10.remove ();
  t.notok (d10.exists (),           "Directory::remove temp/dir.perm file no longer exists");

  tmp.remove ();
  t.notok (tmp.exists (),           "tmp dir removed.");

  return 0;
}
コード例 #5
0
ファイル: threads.c プロジェクト: yang-guo/threads
K threads_init(K x) {
	unsigned char bytes[]={0x01,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0x65,0x13};
	K s=ktn(KG,sizeof(bytes));memcpy(kG(s),bytes,sizeof(bytes));
	if(!okx(s))R krr("serialization");value_fn=d9(s);
	if(!(threadpool=threadpool_create(NUMTHREADS,QUEUESIZE,0)))R krr("threadpool");
	if(pipe(mainloop_pipe)==-1)R krr("pipe");sd1(mainloop_pipe[0],threads_q_callback);R(K)0;}
コード例 #6
0
ファイル: threads.c プロジェクト: yang-guo/threads
static K threads_q_callback(int fd) {
	C b;async_job* job;I rd=read(fd,&job,sizeof(async_job*));K deserialized=knk(1,d9(job->job_result));
	K retval=dot(job->callback,deserialized);if(write(job->free_pipe[1],&b,1)==-1){};
	threads_free_job(job);r0(deserialized);R retval;}
コード例 #7
0
/* Call functions through pointers and and check against expected results.  */
void
test (void)
{

  CHECK_VOID_RESULT (v0 (), 1.0);
  CHECK_VOID_RESULT (v1 (1.0), 2.0);
  CHECK_VOID_RESULT (v5 (5.0, 6.0), 12.0);
  CHECK_VOID_RESULT (v9 (9.0, 10.0), 20.0);
  CHECK_VOID_RESULT (v2 (2.0), 3.0);
  CHECK_VOID_RESULT (v6 (6.0, 7.0), 14.0);
  CHECK_VOID_RESULT (v10 (10.0, 11.0), 22.0);

  CHECK_RESULT (f0 (), 1.0);
  CHECK_RESULT (f1 (1.0), 2.0);
  CHECK_RESULT (f5 (5.0, 6.0), 12.0);
  CHECK_RESULT (f9 (9.0, 10.0), 20.0);
  CHECK_RESULT (f2 (2.0), 3.0);
  CHECK_RESULT (f6 (6.0, 7.0), 14.0);
  CHECK_RESULT (f10 (10.0, 11.0), 22.0);

  CHECK_RESULT (d0 (), 1.0);
  CHECK_RESULT (d1 (1.0), 2.0);
  CHECK_RESULT (d5 (5.0, 6.0), 12.0);
  CHECK_RESULT (d9 (9.0, 10.0), 20.0);
  CHECK_RESULT (d2 (2.0), 3.0);
  CHECK_RESULT (d6 (6.0, 7.0), 14.0);
  CHECK_RESULT (d10 (10.0, 11.0), 22.0);

  CHECK_RESULT (cf0 (), 1.0 + 0.0i);
  CHECK_RESULT (cf1 (1.0), 2.0 + 1.0i);
  CHECK_RESULT (cf5 (5.0, 6.0), 12.0 + 5.0i);
  CHECK_RESULT (cf9 (9.0, 10.0), 20.0 + 9.0i);
  CHECK_RESULT (cf2 (2.0), 3.0 + 2.0i);
  CHECK_RESULT (cf6 (6.0, 7.0), 14.0 + 6.0i);
  CHECK_RESULT (cf10 (10.0, 11.0), 22.0 + 10.0i);

  CHECK_RESULT (cd0 (), 1.0 + 0.0i);
  CHECK_RESULT (cd1 (1.0), 2.0 + 1.0i);
  CHECK_RESULT (cd5 (5.0, 6.0), 12.0 + 5.0i);
  CHECK_RESULT (cd9 (9.0, 10.0), 20.0 + 9.0i);
  CHECK_RESULT (cd2 (2.0), 3.0 + 2.0i);
  CHECK_RESULT (cd6 (6.0, 7.0), 14.0 + 6.0i);
  CHECK_RESULT (cd10 (10.0, 11.0), 22.0 + 10.0i);

  CHECK_VOID_RESULT ((*pv0) (), 1.0);
  CHECK_VOID_RESULT ((*pv1) (1.0), 2.0);
  CHECK_VOID_RESULT ((*pv5) (5.0, 6.0), 12.0);
  CHECK_VOID_RESULT ((*pv9) (9.0, 10.0), 20.0);
  CHECK_VOID_RESULT ((*pv2) (2.0), 3.0);
  CHECK_VOID_RESULT ((*pv6) (6.0, 7.0), 14.0);
  CHECK_VOID_RESULT ((*pv10) (10.0, 11.0), 22.0);

  CHECK_RESULT ((*pf0) (), 1.0);
  CHECK_RESULT ((*pf1) (1.0), 2.0);
  CHECK_RESULT ((*pf5) (5.0, 6.0), 12.0);
  CHECK_RESULT ((*pf9) (9.0, 10.0), 20.0);
  CHECK_RESULT ((*pf2) (2.0), 3.0);
  CHECK_RESULT ((*pf6) (6.0, 7.0), 14.0);
  CHECK_RESULT ((*pf10) (10.0, 11.0), 22.0);

  CHECK_RESULT ((*pd0) (), 1.0);
  CHECK_RESULT ((*pd1) (1.0), 2.0);
  CHECK_RESULT ((*pd5) (5.0, 6.0), 12.0);
  CHECK_RESULT ((*pd9) (9.0, 10.0), 20.0);
  CHECK_RESULT ((*pd2) (2.0), 3.0);
  CHECK_RESULT ((*pd6) (6.0, 7.0), 14.0);
  CHECK_RESULT ((*pd10) (10.0, 11.0), 22.0);

  CHECK_RESULT ((*pcf0) (), 1.0 + 0.0i);
  CHECK_RESULT ((*pcf1) (1.0), 2.0 + 1.0i);
  CHECK_RESULT ((*pcf5) (5.0, 6.0), 12.0 + 5.0i);
  CHECK_RESULT ((*pcf9) (9.0, 10.0), 20.0 + 9.0i);
  CHECK_RESULT ((*pcf2) (2.0), 3.0 + 2.0i);
  CHECK_RESULT ((*pcf6) (6.0, 7.0), 14.0 + 6.0i);
  CHECK_RESULT ((*pcf10) (10.0, 11.0), 22.0 + 10.0i);

  CHECK_RESULT ((*pcd0) (), 1.0 + 0.0i);
  CHECK_RESULT ((*pcd1) (1.0), 2.0 + 1.0i);
  CHECK_RESULT ((*pcd5) (5.0, 6.0), 12.0 + 5.0i);
  CHECK_RESULT ((*pcd9) (9.0, 10.0), 20.0 + 9.0i);
  CHECK_RESULT ((*pcd2) (2.0), 3.0 + 2.0i);
  CHECK_RESULT ((*pcd6) (6.0, 7.0), 14.0 + 6.0i);
  CHECK_RESULT ((*pcd10) (10.0, 11.0), 22.0 + 10.0i);
}
コード例 #8
0
ファイル: directory.t.cpp プロジェクト: gerarldlee/task
int main (int argc, char** argv)
{
  UnitTest t (49);

  // Ensure environment has no influence.
  unsetenv ("TASKDATA");
  unsetenv ("TASKRC");

  Directory tmp ("tmp");
  tmp.create ();
  t.ok (tmp.exists (), "tmp dir created.");

  // Directory (const File&);
  // Directory (const Path&);
  Directory d0 (Path ("tmp"));
  Directory d1 (File ("tmp"));
  Directory d2 (File (Path ("tmp")));
  t.is (d0._data, d1._data, "Directory(std::string) == Directory (File&)");
  t.is (d0._data, d2._data, "Directory(std::string) == Directory (File (Path &))");
  t.is (d1._data, d2._data, "Directory(File&)) == Directory (File (Path &))");

  // Directory (const Directory&);
  Directory d3 (d2);
  t.is (d3._data, Directory::cwd () + "/tmp", "Directory (Directory&)");

  // Directory (const std::string&);
  Directory d4 ("tmp/test_directory");

  // Directory& operator= (const Directory&);
  Directory d5 = d4;
  t.is (d5._data, Directory::cwd () + "/tmp/test_directory", "Directory::operator=");

  // operator (std::string) const;
  t.is ((std::string) d3, Directory::cwd () + "/tmp", "Directory::operator (std::string) const");

  // virtual bool create ();
  t.ok (d5.create (), "Directory::create tmp/test_directory");
  t.ok (d5.exists (), "Directory::exists tmp/test_directory");

  Directory d6 (d5._data + "/dir");
  t.ok (d6.create (), "Directory::create tmp/test_directory/dir");

  File::create (d5._data + "/f0");
  File::create (d6._data + "/f1");

  // std::vector <std::string> list ();
  std::vector <std::string> files = d5.list ();
  std::sort (files.begin (), files.end ());
  t.is ((int)files.size (), 2, "Directory::list 1 file");
  t.is (files[0], Directory::cwd () + "/tmp/test_directory/dir", "file[0] is tmp/test_directory/dir");
  t.is (files[1], Directory::cwd () + "/tmp/test_directory/f0", "file[1] is tmp/test_directory/f0");

  // std::vector <std::string> listRecursive ();
  files = d5.listRecursive ();
  std::sort (files.begin (), files.end ());
  t.is ((int)files.size (), 2, "Directory::list 1 file");
  t.is (files[0], Directory::cwd () + "/tmp/test_directory/dir/f1", "file is tmp/test_directory/dir/f1");
  t.is (files[1], Directory::cwd () + "/tmp/test_directory/f0", "file is tmp/test_directory/f0");

  // virtual bool remove ();
  t.ok (File::remove (d5._data + "/f0"), "File::remove tmp/test_directory/f0");
  t.ok (File::remove (d6._data + "/f1"), "File::remove tmp/test_directory/dir/f1");

  t.ok (d6.remove (), "Directory::remove tmp/test_directory/dir");
  t.notok (d6.exists (), "Directory::exists tmp/test_directory/dir - no");

  t.ok (d5.remove (), "Directory::remove tmp/test_directory");
  t.notok (d5.exists (), "Directory::exists tmp/test_directory - no");

  // bool remove (const std::string&);
  Directory d7 ("tmp/to_be_removed");
  t.ok (d7.create (), "Directory::create tmp/to_be_removed");
  File::create (d7._data + "/f0");
  Directory d8 (d7._data + "/another");
  t.ok (d8.create (), "Directory::create tmp/to_be_removed/another");
  File::create (d8._data + "/f1");
  t.ok (d7.remove (), "Directory::remove tmp/to_be_removed");
  t.notok (d7.exists (), "Directory tmp/to_be_removed gone");

  // static std::string cwd ();
  std::string cwd = Directory::cwd ();
  t.ok (cwd.length () > 0, "Directory::cwd returned a value");

  // bool parent (std::string&) const;
  Directory d9 ("/one/two/three/four.txt");
  t.ok (d9.up (),                   "parent /one/two/three/four.txt --> true");
  t.is (d9._data, "/one/two/three", "parent /one/two/three/four.txt --> /one/two/three");
  t.ok (d9.up (),                   "parent /one/two/three --> true");
  t.is (d9._data, "/one/two",       "parent /one/two/three --> /one/two");
  t.ok (d9.up (),                   "parent /one/two --> true");
  t.is (d9._data, "/one",           "parent /one/two --> /one");
  t.ok (d9.up (),                   "parent /one --> true");
  t.is (d9._data, "/",              "parent /one --> /");
  t.notok (d9.up (),                "parent / --> false");

  // Test permissions.
  umask (0022);
  Directory d10 ("tmp/dir.perm");
  d10.create (0750);
  t.ok (d10.exists (),               "Directory::create perm file exists");
  mode_t m = d10.mode ();
  t.ok    (m & S_IFDIR,             "Directory::mode tmp/dir.perm S_IFDIR good");
  t.ok    (m & S_IRUSR,             "Directory::mode tmp/dir.perm r-------- good");
  t.ok    (m & S_IWUSR,             "Directory::mode tmp/dir.perm -w------- good");
  t.ok    (m & S_IXUSR,             "Directory::mode tmp/dir.perm --x------ good");
  t.ok    (m & S_IRGRP,             "Directory::mode tmp/dir.perm ---r----- good");
  t.notok (m & S_IWGRP,             "Directory::mode tmp/dir.perm ----w---- good");
  t.ok    (m & S_IXGRP,             "Directory::mode tmp/dir.perm -----x--- good");
  t.notok (m & S_IROTH,             "Directory::mode tmp/dir.perm ------r-- good");
  t.notok (m & S_IWOTH,             "Directory::mode tmp/dir.perm -------w- good");
  t.notok (m & S_IXOTH,             "Directory::mode tmp/dir.perm --------x good");
  d10.remove ();
  t.notok (d10.exists (),           "Directory::remove temp/dir.perm file no longer exists");

  tmp.remove ();
  t.notok (tmp.exists (),           "tmp dir removed.");

  return 0;
}