Skip to content

Commit

Permalink
vector
Browse files Browse the repository at this point in the history
vector class first tests successfull
  • Loading branch information
tnut committed May 1, 2024
1 parent 6824c2a commit 88f21d2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ a.out
*.gch
ChangeLog
archives
tests/test
tests/filesList
documentation/htdocs
documentation/Doxyfile
documentation/latex
Expand Down
37 changes: 34 additions & 3 deletions src/vector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,51 @@
namespace cards {
vector::vector() {
m_capacity = 15;
m_size = 1;

m_items = (char**)malloc(sizeof m_items * m_capacity);
m_size = 0;
}
vector::vector(const unsigned int capacity){
m_capacity = capacity;
m_items = (char**)malloc(sizeof m_items * m_capacity);
m_size = 0;
}
vector::vector(char * element) {
m_capacity = 15;
m_items = (char**)malloc(sizeof m_items * m_capacity);
m_size = 1;
m_items[0] = strdup(element);
}
void vector::push_back(char * element) {
if ( m_capacity == m_size) {
m_capacity = m_size * 2;
m_items = (char **)realloc( m_items, sizeof m_items * m_capacity );
if ( m_items == NULL ) {
printf("Cannot reallocate m_items: %d", &m_items);
return;
}
}
m_items[ m_size ] = strdup(element);
++m_size;
}
const char* vector::value(const unsigned int index) {
if (index > m_size - 1)
return m_items[m_size - 1];
return m_items[index];
}
const unsigned int vector::size(){
return m_size;
}
const unsigned int vector::capacity(){
return m_capacity;
}

vector::~vector(){
for (unsigned int i=0; i < m_size;i++) {
if (m_items[i] != nullptr) {
free(m_items[i]);
m_items[i] = nullptr;
}
}
}
} // end of cards namespace

// vim:set ts=2 :
Expand Down
12 changes: 8 additions & 4 deletions src/vector.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#pragma once
#include "string_utils.h"

namespace cards {

class vector
{
public:
vector();
vector(const unsigned int capacity);
~vector(){};
vector(char * element);
~vector();

void push_back(const void * element);
const char* value(const unsigned int index);
void push_back(char * element);
const unsigned int size();
const unsigned int capacity();


private:
unsigned int m_capacity;
char **m_items;
unsigned int m_size;

unsigned int m_capacity;
};
} // end of cards namespace

Expand Down
8 changes: 7 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

include ../Makefile.inc

all: uname test1 test2 list1
all: uname test1 test2 list1 vector

uname:
$(CXX) -DNDEBUG -std=c++11 -O2 -Werror -pedantic -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -fPIC -c -o $@.o $@.cxx
Expand All @@ -40,6 +40,11 @@ test2:
$(CXX) $@.o -o $@ -L"../src" -lcards -lcurl -larchive
rm $@.o

vector:
$(CXX) -DNDEBUG -std=c++11 -O2 -Werror -pedantic -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -fPIC -c -o $@.o $@.cxx
$(CXX) $@.o -o $@ -L"../src" -lcards -lcurl -larchive
rm $@.o

list1:
$(CXX) -DNDEBUG -std=c++11 -O2 -Werror -pedantic -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -fPIC -c -o $@.o $@.cxx
$(CXX) $@.o -o $@ -L"../src" -lcards -lcurl -larchive
Expand All @@ -49,6 +54,7 @@ clean:
rm -f list1
rm -f test1
rm -f test2
rm -f vector
rm -f uname

# End of file
28 changes: 28 additions & 0 deletions tests/vector.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../src/libcards.h"

int main() {
FILE* fp = fopen("filesList", "r");
if (!fp) {
printf("Cannot open file filesList\n");
return -1;
}

char line[BUFSIZ];
cards::vector myVector(1);

while (fgets(line, BUFSIZ, fp)) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
myVector.push_back(line);
}
fclose(fp);
printf("Capacity of myVector: %u\n",myVector.capacity());
printf("Size of myVector: %u\n",myVector.size());

for (unsigned int i = 0; i < myVector.size(); ++i )
printf("%s\n",myVector.value(i));

printf("First Value: %s\n",myVector.value(0));
printf("Last Value: %s\n",myVector.value(myVector.size()));
return 0;
}

0 comments on commit 88f21d2

Please sign in to comment.