Exemplo n.º 1
0
/**
 * Funkce vytiskne znak na místo kurzoru. Funkce není prováděna ve "vlákně".
 */
void printc(char c){
  d10();
  PORTA = c;
  PORTB = ((PORTB & 0xf8) | (5 & 0x07));
  d10();
  PORTB = ((PORTB & 0xf8) | (1 & 0x07));
  d10();
  PORTB = ((PORTB & 0xf8) | (5 & 0x07));
}
Exemplo n.º 2
0
unsigned int DiceBox::d10(const unsigned short int n)
{
  unsigned int result = 0;
  unsigned int i;
  for (i=0; i<n; i=i+1)
  {
    result += d10();
  }
  return result;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
void render() {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	zviewpointSetupView();

	const int MAX_TRIS = 1000000;
	static Triangle tris[MAX_TRIS];
	int triCount = 0;
	
	const int steps = 60;
	const float stepsf = (float)steps;
	
	Rod r0( FVec3(-0.3f,0.f,0.f), FVec3(+0.f,0.f,0.f), Cloud_r );
	Rod r1( FVec3(0.f,0.f,0.f), FVec3(0.2f,0.5f,0.f), Cloud_r );
	Sphere s0( FVec3(-.5f, -.5f, 0.f), 0.25f );
	
	Surface *surfaces[] = {
		(Surface *)&r0,
		(Surface *)&r1,
		(Surface *)&s0
	};
	int surfacesCount = sizeof(surfaces) / sizeof(surfaces[0]);

	float f[steps][steps][steps];
	for( int xi=0; xi<steps; xi++ ) {
		for( int yi=0; yi<steps; yi++ ) {
			for( int zi=0; zi<steps; zi++ ) {
				FVec3 p( 2.f*(float)xi/stepsf-1.f, 2.f*(float)yi/stepsf-1.f, 2.f*(float)zi/stepsf-1.f );
				f[xi][yi][zi] = 0.f;
				for( int i=0; i<surfacesCount; i++ ) {
					f[xi][yi][zi] += surfaces[i]->surface( p );
				}
			}
		}
	}

	for( int xi=0; xi<steps-1; xi++ ) {
		for( int yi=0; yi<steps-1; yi++ ) {
			for( int zi=0; zi<steps-1; zi++ ) {
				GridCell g;
				float x0 = 2.f*(float)(xi+0)/stepsf-1.f;
				float x1 = 2.f*(float)(xi+1)/stepsf-1.f;
				float y0 = 2.f*(float)(yi+0)/stepsf-1.f;
				float y1 = 2.f*(float)(yi+1)/stepsf-1.f;
				float z0 = 2.f*(float)(zi+0)/stepsf-1.f;
				float z1 = 2.f*(float)(zi+1)/stepsf-1.f;
				g.p[0] = FVec3( x0, y0, z0 );
				g.p[1] = FVec3( x1, y0, z0 );
				g.p[2] = FVec3( x1, y1, z0 );
				g.p[3] = FVec3( x0, y1, z0 );
				g.p[4] = FVec3( x0, y0, z1 );
				g.p[5] = FVec3( x1, y0, z1 );
				g.p[6] = FVec3( x1, y1, z1 );
				g.p[7] = FVec3( x0, y1, z1 );
				g.val[0] =	f[xi+0][yi+0][zi+0];
				g.val[1] =	f[xi+1][yi+0][zi+0];
				g.val[2] =	f[xi+1][yi+1][zi+0];
				g.val[3] =	f[xi+0][yi+1][zi+0];
				g.val[4] =	f[xi+0][yi+0][zi+1];
				g.val[5] =	f[xi+1][yi+0][zi+1];
				g.val[6] =	f[xi+1][yi+1][zi+1];
				g.val[7] =	f[xi+0][yi+1][zi+1];

				triCount += polygonise( g, Cloud_r, &tris[triCount] );
				assert( triCount < MAX_TRIS );
			}
		}
	}

	DiscWarp dw( FVec3(-0.2f,0.f,0.f), FVec3(1.f,0.f,0.f) );
	for( int i=0; i<triCount; i++ ) {
		tris[i].p[0] = dw.warp( tris[i].p[0] );
		tris[i].p[1] = dw.warp( tris[i].p[1] );
		tris[i].p[2] = dw.warp( tris[i].p[2] );
	}
	
	dw.draw();
	
	static unsigned int triList[MAX_TRIS*3];
	static FVec3 normals[MAX_TRIS*3];

	ZGLLight light0;
	light0.resetToDefaults();
	light0.active = 1;
	light0.ambient[0] = 0.05f;
	light0.ambient[1] = 0.05f;
	light0.ambient[2] = 0.05f;
	light0.diffuse[0] = 0.8f;
	light0.diffuse[1] = 0.2f;
	light0.diffuse[2] = 0.2f;
	light0.dir[0] = 10.f;
	light0.dir[1] = 0.f;
	light0.dir[2] = 0.f;
	light0.makeDirectional();
	light0.setLightNumber( 0 );

	ZGLLight light1;
	light1.resetToDefaults();
	light1.active = 1;
	light1.ambient[0] = 0.05f;
	light1.ambient[1] = 0.05f;
	light1.ambient[2] = 0.05f;
	light1.diffuse[0] = 0.2f;
	light1.diffuse[1] = 0.2f;
	light1.diffuse[2] = 0.8f;
	light1.dir[0] = -10.f;
	light1.dir[1] = -10.f;
	light1.dir[2] = 0.f;
	light1.makeDirectional();
	light1.setLightNumber( 1 );

	glEnable( GL_COLOR_MATERIAL );
	glEnable( GL_LIGHTING );
	glEnable( GL_LIGHT0 );
	glEnable( GL_LIGHT1 );
	glEnable( GL_DEPTH_TEST );
	glEnable( GL_NORMALIZE );
	light0.setGL();
	light1.setGL();

	for( int i=0; i<triCount; i++ ) {
		FVec3 d10( tris[i].p[1] );
		d10.sub( tris[i].p[0] );
		FVec3 d20( tris[i].p[2] );
		d20.sub( tris[i].p[0] );
		normals[i*3+0] = d20;
		normals[i*3+0].cross( d10 );

		FVec3 d01( tris[i].p[0] );
		d01.sub( tris[i].p[1] );
		FVec3 d21( tris[i].p[2] );
		d21.sub( tris[i].p[1] );
		normals[i*3+1] = d01;
		normals[i*3+1].cross( d21 );

		FVec3 d02( tris[i].p[0] );
		d02.sub( tris[i].p[2] );
		FVec3 d12( tris[i].p[1] );
		d12.sub( tris[i].p[2] );
		normals[i*3+2] = d12;
		normals[i*3+2].cross( d02 );
	
		triList[i*3+0] = i*3+0;
		triList[i*3+1] = i*3+1;
		triList[i*3+2] = i*3+2;
	}
	
	glColor3ub( 255, 255, 255 );
	glEnableClientState( GL_VERTEX_ARRAY );
	glVertexPointer( 3, GL_FLOAT, 0, &tris[0] );
	glEnableClientState( GL_NORMAL_ARRAY );
	glNormalPointer( GL_FLOAT, 0, &normals[0] );
	glDrawElements( GL_TRIANGLES, triCount*3, GL_UNSIGNED_INT, &triList[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);
}
Exemplo n.º 6
0
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;
}