aboutsummaryrefslogtreecommitdiff
path: root/tests/sha1/driver.cxx
blob: cb648dbb94689c08370e2bc855de50f74c4b44a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// file      : tests/sha1/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_lib_modules_ts
#include <string>
#include <cstddef> // size_t
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.sha1;
import butl.path;
import butl.fdstream;
import butl.filesystem;
#else
#include <libbutl/sha1.mxx>
#include <libbutl/path.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/filesystem.mxx> // auto_rmfile
#endif

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;

int
main ()
{
  assert (string (sha1 ().string ()) ==
          "da39a3ee5e6b4b0d3255bfef95601890afd80709");

  assert (string (sha1 ("").string ()) !=
          "da39a3ee5e6b4b0d3255bfef95601890afd80709");

  assert (string (sha1 ("123", 3).string ()) ==
          "40bd001563085fc35165329ea1ff5c5ecbdbbeef");

  {
    string s;
    for (size_t i (0); i < 1024; ++i)
      s += "0123456789";

    path p (path::temp_path ("butl-sha1"));

    auto_rmfile pr;       // Always remove the file after the stream is closed.
    ofdstream os (p);
    pr = auto_rmfile (p);

    os << s;
    os.close ();

    ifdstream is (p);

    assert (string (sha1 (is).string ()) ==
            sha1 (s.c_str (), s.size ()).string ());

    assert (is.eof ());
    is.close ();
  }

  {
    sha1 h ("123");
    assert (string (h.string ()) ==
            "cc320164df1a2130045a28f08d3b88bc5bbcc43a");

    assert (h.abbreviated_string (10) == "cc320164df");
    assert (h.abbreviated_string (41) == h.string ());
  }

  {
    sha1 h;
    h.append ("1");
    h.append (string ("2"));
    h.append ("3", 1);

    auto& b (h.binary ());
    assert (b[0] == 0x58 && b[19] == 0xfd);

    assert (string (h.string ()) ==
            "58c596bafad8d007952934af1db9abc5401d4dfd");
  }
}