-
Notifications
You must be signed in to change notification settings - Fork 6
/
StringNDriver.cpp
103 lines (91 loc) · 2.5 KB
/
StringNDriver.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* StringNDriver.cpp
2015-11-08 - 2018-06-30
Esp. Ing. José María Sola
Profesor
UTN FRBA */
#include <string>
#include <array>
#include <iostream>
#include <cassert>
#include "StringN.h"
template <std::size_t N>
void PrintSizesAndContents(std::string, String<N>, std::string);
int main(){
{ // Tests
{ // array más corto que string
std::array<char,5> a = PackString("Hello, World!");
assert( "Hello" == UnpackString(a) );
}
{ // String<N> más corto que string
String<4> s = PackString("Hello, World!");
assert( "Hell" == UnpackString(s) );
}
{ // String<N> igual de largo que string
String<13> s = PackString("Hello, World!");
assert( "Hello, World!" == UnpackString(s) );
}
{ // String<N> más largo que string
String<42> s = PackString("Hello, World!");
assert( "Hello, World!" == UnpackString(s) );
}
}
{ // Ejemplos
using std::array;
using std::string;
{ // String<N>
string s{"Hello, World!"};
String<5> a = PackString(s);
string t{UnpackString(a)};
assert( t == "Hello" );
}
{ // array<char,N>
string s{"Hello, World!"};
array<char,5> a = PackString(s);
string t{UnpackString(a)};
assert( t == "Hello");
}
{ // String<N> más corto que string
string s{"Texto de largo mayor a límite de registro."};
String<12> a = PackString(s); // array<char,12>
auto t{UnpackString(a)}; // desempaque todo lo que se pudo guardar
assert(s.compare(0, 12, t) == 0);
PrintSizesAndContents(s,a,t);
}
{ // String<N> igual de largo que string
string s{"abcd"}; // string type
String<4> a = PackString(s); // array<char,4>
auto t{UnpackString(a)};
assert(s == t);
PrintSizesAndContents(s,a,t);
}
{ // String<N> más largo que string
string s{"xyz"};
array<char,7> a = PackString(s);
auto t{UnpackString(a)};
assert(s == t);
PrintSizesAndContents(s,a,t);
}
}
}
template <std::size_t N>
void PrintString(const String<N>&);
template <std::size_t N>
void PrintSizesAndContents(std::string s, String<N> a, std::string t){
std::cout
<< "s : " << s << "\n"
<< "sizeof s: " << sizeof s << "\n"
<< "length s: " << s.length() << "\n"
<< "a : "; PrintString(a); std::cout << "\n"
<< "sizeof a: " << sizeof a << "\n"
<< "t : " << t << "\n"
<< "sizeof t: " << sizeof t << "\n"
<< "length t: " << t.length() << "\n\n";
}
template <std::size_t N>
void PrintString(const String<N>& s){
for(auto c : s){
if(c=='\0')
break;
std::cout << c;
}
}