Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix print group #126

Merged
merged 3 commits into from
Aug 19, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Format:

==================

2013-08-18 (Kenneth Geisshirt)
! Group::to_string formatting was incorrect. See https://app.asana.com/0/1441391972580/5659532773181.


2013-08-03 (Kristian Spangsege)
+ Table::find_sorted_int() replaced by Table::lower_bound_int() and Table::upper_bound_int() as these are standardized and provide more flexibility.
Expand Down
30 changes: 13 additions & 17 deletions src/tightdb/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <new>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>

#include <tightdb/terminate.hpp>
Expand Down Expand Up @@ -389,7 +390,7 @@ size_t Group::commit(size_t current_version, size_t readlock_version, bool persi
TIGHTDB_ASSERT(readlock_version <= current_version);

// FIXME: Under what circumstances can this even happen????
// FIXME: What about when a user owned read-only buffer is attached?
// FIXME: What about when a user owned read-only buffer is attached?
if (!m_alloc.is_attached()) throw runtime_error("Cannot persist");

// If we have an empty db file, we can just serialize directly
Expand Down Expand Up @@ -535,39 +536,34 @@ bool Group::operator==(const Group& g) const
void Group::to_string(ostream& out) const
{
// Calculate widths
size_t name_width = 6;
size_t rows_width = 4;
size_t index_width = 4;
size_t name_width = 10;
size_t rows_width = 6;
size_t count = size();
for (size_t i = 0; i < count; ++i) {
StringData name = get_table_name(i);
if (name_width < name.size()) name_width = name.size();

ConstTableRef table = get_table(name);
size_t row_count = table->size();
if (rows_width < row_count) rows_width = row_count;
if (rows_width < row_count) rows_width = row_count; // FIXME: should be the number of digits in row_count: floor(log10(row_count+1))
}


// Print header
out << " ";
out.width(name_width);
out << "tables" << " ";
out.width(rows_width);
out << "rows\n";
out << std::setw(index_width+1) << std::left << " ";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I have advocated a lot for "std::" lately, but here they are really superfluous due to the "using namespace std" at the beginning of the file. As a general rule, I suggest we only use "std::" in header files.

out << std::setw(name_width+1) << std::left << "tables";
out << std::setw(rows_width) << std::left << "rows" << endl;

// Print tables
for (size_t i = 0; i < count; ++i) {
StringData name = get_table_name(i);
ConstTableRef table = get_table(name);
size_t row_count = table->size();

out << i << " ";
out.width(name_width);
out.setf(ostream::left, ostream::adjustfield);
out << name;
out << " ";
out.width(rows_width);
out.unsetf(ostream::adjustfield);
out << row_count << endl;
out << std::setw(index_width) << std::right << i << " ";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous "std::".

out << std::setw(name_width) << std::left << name.data() << " ";
out << std::setw(rows_width) << std::left << row_count << endl;
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/testgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,21 @@ TEST(Group_toJSON)
CHECK(str.length() > 0);
}

TEST(Group_toString)
{
Group g;
TestTableGroup::Ref table = g.get_table<TestTableGroup>("test");

table->add("jeff", 1, true, Wed);
table->add("jim", 1, true, Wed);
std::ostringstream ss;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous "std::".

ss.sync_with_stdio(false); // for performance
g.to_string(ss);
const std::string str = ss.str();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous "std::".

CHECK(str.length() > 0);
CHECK_EQUAL(" tables rows \n 0 test 2 \n", str.c_str());
}

TEST(Group_Index_String)
{
Group toMem;
Expand Down