Use countof to get number of array elements

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-02-23 20:19:52 +01:00
parent 7097dfd41c
commit 0b8e937655
12 changed files with 56 additions and 59 deletions

View File

@ -54,8 +54,6 @@ const double kMaxRowSize = 2.5;
// Number of filled columns required to form a strong table row.
// For small tables, this is an absolute number.
const double kGoodRowNumberOfColumnsSmall[] = { 2, 2, 2, 2, 2, 3, 3 };
const int kGoodRowNumberOfColumnsSmallSize =
sizeof(kGoodRowNumberOfColumnsSmall) / sizeof(double) - 1;
// For large tables, it is a relative number
const double kGoodRowNumberOfColumnsLarge = 0.7;
// The amount of area that must be covered in a cell by ColPartitions to
@ -1055,11 +1053,12 @@ bool TableRecognizer::IsWeakTableRow(StructuredTable* table, int row) {
if (!table->VerifyRowFilled(row))
return false;
double threshold = 0.0;
if (table->column_count() > kGoodRowNumberOfColumnsSmallSize)
threshold = table->column_count() * kGoodRowNumberOfColumnsLarge;
else
double threshold;
if (table->column_count() < countof(kGoodRowNumberOfColumnsSmall)) {
threshold = kGoodRowNumberOfColumnsSmall[table->column_count()];
} else {
threshold = table->column_count() * kGoodRowNumberOfColumnsLarge;
}
return table->CountFilledCellsInRow(row) < threshold;
}

View File

@ -282,7 +282,7 @@ TEST_F(TesseractTest, InitConfigOnlyTest) {
const char* langs[] = {"eng", "chi_tra", "jpn", "vie"};
std::unique_ptr<tesseract::TessBaseAPI> api;
CycleTimer timer;
for (size_t i = 0; i < ARRAYSIZE(langs); ++i) {
for (size_t i = 0; i < countof(langs); ++i) {
api.reset(new tesseract::TessBaseAPI);
timer.Restart();
EXPECT_EQ(0, api->Init(TessdataPath().c_str(), langs[i],
@ -296,7 +296,7 @@ TEST_F(TesseractTest, InitConfigOnlyTest) {
vars_vec.push_back("tessedit_init_config_only");
vars_values.push_back("1");
LOG(INFO) << "Switching to config only initialization:";
for (size_t i = 0; i < ARRAYSIZE(langs); ++i) {
for (size_t i = 0; i < countof(langs); ++i) {
api.reset(new tesseract::TessBaseAPI);
timer.Restart();
EXPECT_EQ(0, api->Init(TessdataPath().c_str(), langs[i],

View File

@ -44,7 +44,7 @@ class CommandlineflagsTest : public ::testing::Test {
TEST_F(CommandlineflagsTest, RemoveFlags) {
const char* const_argv[] = {"Progname", "--foo_int", "3", "file1.h",
"file2.h"};
int argc = ARRAYSIZE(const_argv);
int argc = countof(const_argv);
char** argv = const_cast<char**>(const_argv);
tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
@ -58,7 +58,7 @@ TEST_F(CommandlineflagsTest, RemoveFlags) {
#if 0 // TODO: this test needs an update (it currently fails).
TEST_F(CommandlineflagsTest, PrintUsageAndExit) {
const char* argv[] = { "Progname", "--help" };
EXPECT_EXIT(TestParser("Progname [flags]", ARRAYSIZE(argv), argv),
EXPECT_EXIT(TestParser("Progname [flags]", countof(argv), argv),
::testing::ExitedWithCode(0),
"USAGE: Progname \\[flags\\]");
}
@ -66,32 +66,32 @@ TEST_F(CommandlineflagsTest, PrintUsageAndExit) {
TEST_F(CommandlineflagsTest, ExitsWithErrorOnInvalidFlag) {
const char* argv[] = {"", "--test_nonexistent_flag"};
EXPECT_EXIT(TestParser(ARRAYSIZE(argv), argv), ::testing::ExitedWithCode(1),
EXPECT_EXIT(TestParser(countof(argv), argv), ::testing::ExitedWithCode(1),
"ERROR: Non-existent flag");
}
TEST_F(CommandlineflagsTest, ParseIntegerFlags) {
const char* argv[] = {"", "--foo_int=3", "--bar_int", "-4"};
TestParser(ARRAYSIZE(argv), argv);
TestParser(countof(argv), argv);
EXPECT_EQ(3, FLAGS_foo_int);
EXPECT_EQ(-4, FLAGS_bar_int);
const char* arg_no_value[] = {"", "--bar_int"};
EXPECT_EXIT(TestParser(ARRAYSIZE(arg_no_value), arg_no_value),
EXPECT_EXIT(TestParser(countof(arg_no_value), arg_no_value),
::testing::ExitedWithCode(1), "ERROR");
const char* arg_invalid_value[] = {"", "--bar_int", "--foo_int=3"};
EXPECT_EXIT(TestParser(ARRAYSIZE(arg_invalid_value), arg_invalid_value),
EXPECT_EXIT(TestParser(countof(arg_invalid_value), arg_invalid_value),
::testing::ExitedWithCode(1), "ERROR");
const char* arg_bad_format[] = {"", "--bar_int="};
EXPECT_EXIT(TestParser(ARRAYSIZE(arg_bad_format), arg_bad_format),
EXPECT_EXIT(TestParser(countof(arg_bad_format), arg_bad_format),
::testing::ExitedWithCode(1), "ERROR");
}
TEST_F(CommandlineflagsTest, ParseDoubleFlags) {
const char* argv[] = {"", "--foo_double=3.14", "--bar_double", "1.2"};
TestParser(ARRAYSIZE(argv), argv);
TestParser(countof(argv), argv);
EXPECT_EQ(3.14, FLAGS_foo_double);
EXPECT_EQ(1.2, FLAGS_bar_double);
@ -107,7 +107,7 @@ TEST_F(CommandlineflagsTest, ParseDoubleFlags) {
TEST_F(CommandlineflagsTest, ParseStringFlags) {
const char* argv[] = {"", "--foo_string=abc", "--bar_string", "def"};
TestParser(ARRAYSIZE(argv), argv);
TestParser(countof(argv), argv);
EXPECT_STREQ("abc", FLAGS_foo_string.c_str());
EXPECT_STREQ("def", FLAGS_bar_string.c_str());
@ -126,7 +126,7 @@ TEST_F(CommandlineflagsTest, ParseBoolFlags) {
const char* argv[] = {"", "--foo_bool=true", "--bar_bool=1"};
FLAGS_foo_bool.set_value(false);
FLAGS_bar_bool.set_value(false);
TestParser(ARRAYSIZE(argv), argv);
TestParser(countof(argv), argv);
// Verify changed value
EXPECT_TRUE(FLAGS_foo_bool);
EXPECT_TRUE(FLAGS_bar_bool);
@ -152,7 +152,7 @@ TEST_F(CommandlineflagsTest, ParseBoolFlags) {
TEST_F(CommandlineflagsTest, ParseOldFlags) {
EXPECT_STREQ("", FLAGS_q.c_str());
const char* argv[] = {"", "-q", "text"};
TestParser(ARRAYSIZE(argv), argv);
TestParser(countof(argv), argv);
EXPECT_STREQ("text", FLAGS_q.c_str());
}
} // namespace

View File

@ -35,7 +35,7 @@ class HeapTest : public testing::Test {
virtual ~HeapTest();
// Pushes the test data onto both the heap and the KDVector.
void PushTestData(GenericHeap<IntKDPair>* heap, KDVector* v) {
for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
for (size_t i = 0; i < countof(test_data); ++i) {
IntKDPair pair(test_data[i], i);
heap->Push(&pair);
v->push_back(pair);
@ -138,7 +138,7 @@ TEST_F(HeapTest, RevalueTest) {
GenericHeap<PtrPair> heap;
GenericVector<PtrPair> v;
// Push the test data onto both the heap and the vector.
for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
for (size_t i = 0; i < countof(test_data); ++i) {
PtrPair h_pair;
h_pair.key() = test_data[i];
PtrPair v_pair;

View File

@ -59,8 +59,6 @@ public:
}
};
#define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0]))
// /usr/include/tensorflow/core/platform/default/logging.h defines the CHECK* macros.
#if !defined(CHECK)
#define CHECK(condition) \

View File

@ -85,7 +85,7 @@ TEST_F(LigatureTableTest, TestCustomLigatures) {
"act", "a\uE003", "publiſh", "publi\uE006", "ſince",
"\uE007nce", "aſleep", "a\uE008eep", "neceſſary", "nece\uE009ary",
};
for (size_t i = 0; i < ARRAYSIZE(kTestCases); i += 2) {
for (size_t i = 0; i < countof(kTestCases); i += 2) {
EXPECT_STREQ(kTestCases[i + 1],
lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
EXPECT_STREQ(kTestCases[i],
@ -101,7 +101,7 @@ TEST_F(LigatureTableTest, TestRemovesCustomLigatures) {
"\uE003ion",
"fiction",
};
for (size_t i = 0; i < ARRAYSIZE(kTestCases); i += 3) {
for (size_t i = 0; i < countof(kTestCases); i += 3) {
EXPECT_STREQ(kTestCases[i + 1],
lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
EXPECT_STREQ(kTestCases[i + 2],

View File

@ -112,13 +112,13 @@ TEST(NormstrngsTest, DetectsCorrectText) {
}
TEST(NormstrngsTest, DetectsIncorrectText) {
for (size_t i = 0; i < ARRAYSIZE(kBadlyFormedHinWords); ++i) {
for (size_t i = 0; i < countof(kBadlyFormedHinWords); ++i) {
EXPECT_FALSE(NormalizeUTF8String(UnicodeNormMode::kNFKC, OCRNorm::kNone,
GraphemeNorm::kNormalize,
kBadlyFormedHinWords[i], nullptr))
<< kBadlyFormedHinWords[i];
}
for (size_t i = 0; i < ARRAYSIZE(kBadlyFormedThaiWords); ++i) {
for (size_t i = 0; i < countof(kBadlyFormedThaiWords); ++i) {
EXPECT_FALSE(NormalizeUTF8String(UnicodeNormMode::kNFKC, OCRNorm::kNone,
GraphemeNorm::kNormalize,
kBadlyFormedThaiWords[i], nullptr))

View File

@ -28,7 +28,7 @@ class NthItemTest : public testing::Test {
virtual ~NthItemTest();
// Pushes the test data onto the KDVector.
void PushTestData(KDVector* v) {
for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
for (size_t i = 0; i < countof(test_data); ++i) {
IntKDPair pair(test_data[i], i);
v->push_back(pair);
}
@ -69,7 +69,7 @@ TEST_F(NthItemTest, BoringTest) {
KDVector v;
// Push the test data onto the KDVector.
int test_data[] = {8, 8, 8, 8, 8, 7, 7, 7, 7};
for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
for (size_t i = 0; i < countof(test_data); ++i) {
IntKDPair pair(test_data[i], i);
v.push_back(pair);
}

View File

@ -169,7 +169,7 @@ TEST_F(PangoFontInfoTest, CanDropUncoveredChars) {
"\u200C", // U+200C (ZWJ)
"\u200D" // U+200D (ZWNJ)
};
for (size_t i = 0; i < ARRAYSIZE(kJoiners); ++i) {
for (size_t i = 0; i < countof(kJoiners); ++i) {
word = kJoiners[i];
EXPECT_EQ(0, font_info_.DropUncoveredChars(&word));
EXPECT_STREQ(kJoiners[i], word.c_str());
@ -253,7 +253,7 @@ TEST_F(FontUtilsTest, DoesFindBestFonts) {
std::string best_list = FontUtils::BestFonts(ch_map, &font_flags);
EXPECT_TRUE(best_list.size());
// All fonts except Lohit Hindi should render English text.
EXPECT_EQ(ARRAYSIZE(kExpectedFontNames) - 1, font_flags.size());
EXPECT_EQ(countof(kExpectedFontNames) - 1, font_flags.size());
CountUnicodeChars(kKorText, &ch_map);
best_list = FontUtils::BestFonts(ch_map, &font_flags);
@ -319,7 +319,7 @@ TEST_F(FontUtilsTest, GetAllRenderableCharacters) {
// Check that none of the included fonts cover the Mongolian or Ogham space
// characters.
for (size_t f = 0; f < ARRAYSIZE(kExpectedFontNames); ++f) {
for (size_t f = 0; f < countof(kExpectedFontNames); ++f) {
SCOPED_TRACE(absl::StrCat("Testing ", kExpectedFontNames[f]));
FontUtils::GetAllRenderableCharacters(kExpectedFontNames[f], &unicode_mask);
#if 0 // TODO: check fails because DejaVu Sans Ultra-Light supports ogham

View File

@ -243,7 +243,7 @@ const TextAndModel kTwoSimpleParagraphs[] = {
TEST(ParagraphsTest, TestSimpleParagraphDetection) {
TestParagraphDetection(kTwoSimpleParagraphs,
ABSL_ARRAYSIZE(kTwoSimpleParagraphs));
countof(kTwoSimpleParagraphs));
}
const TextAndModel kFewCluesWithCrown[] = {
@ -260,7 +260,7 @@ const TextAndModel kFewCluesWithCrown[] = {
TEST(ParagraphsTest, TestFewCluesWithCrown) {
TestParagraphDetection(kFewCluesWithCrown,
ABSL_ARRAYSIZE(kFewCluesWithCrown));
countof(kFewCluesWithCrown));
}
const TextAndModel kCrownedParagraph[] = {
@ -278,7 +278,7 @@ const TextAndModel kCrownedParagraph[] = {
};
TEST(ParagraphsTest, TestCrownParagraphDetection) {
TestParagraphDetection(kCrownedParagraph, ABSL_ARRAYSIZE(kCrownedParagraph));
TestParagraphDetection(kCrownedParagraph, countof(kCrownedParagraph));
}
const TextAndModel kFlushLeftParagraphs[] = {
@ -298,7 +298,7 @@ const TextAndModel kFlushLeftParagraphs[] = {
TEST(ParagraphsText, TestRealFlushLeftParagraphs) {
TestParagraphDetection(kFlushLeftParagraphs,
ABSL_ARRAYSIZE(kFlushLeftParagraphs));
countof(kFlushLeftParagraphs));
}
const TextAndModel kSingleFullPageContinuation[] = {
@ -320,7 +320,7 @@ const TextAndModel kSingleFullPageContinuation[] = {
TEST(ParagraphsTest, TestSingleFullPageContinuation) {
const TextAndModel* correct = kSingleFullPageContinuation;
int num_rows = ABSL_ARRAYSIZE(kSingleFullPageContinuation);
int num_rows = countof(kSingleFullPageContinuation);
std::vector<RowInfo> row_infos;
GenericVector<PARA*> row_owners;
PARA_LIST paragraphs;
@ -346,7 +346,7 @@ const TextAndModel kRightAligned[] = {
};
TEST(ParagraphsTest, TestRightAlignedParagraph) {
TestParagraphDetection(kRightAligned, ABSL_ARRAYSIZE(kRightAligned));
TestParagraphDetection(kRightAligned, countof(kRightAligned));
}
const TextAndModel kTinyParagraphs[] = {
@ -368,7 +368,7 @@ const TextAndModel kTinyParagraphs[] = {
};
TEST(ParagraphsTest, TestTinyParagraphs) {
TestParagraphDetection(kTinyParagraphs, ABSL_ARRAYSIZE(kTinyParagraphs));
TestParagraphDetection(kTinyParagraphs, countof(kTinyParagraphs));
}
const TextAndModel kComplexPage1[] = {
@ -418,7 +418,7 @@ const TextAndModel kComplexPage1[] = {
};
TEST(ParagraphsTest, TestComplexPage1) {
TestParagraphDetection(kComplexPage1, ABSL_ARRAYSIZE(kComplexPage1));
TestParagraphDetection(kComplexPage1, countof(kComplexPage1));
}
// The same as above, but wider.
@ -467,7 +467,7 @@ const TextAndModel kComplexPage2[] = {
};
TEST(ParagraphsTest, TestComplexPage2) {
TestParagraphDetection(kComplexPage2, ABSL_ARRAYSIZE(kComplexPage2));
TestParagraphDetection(kComplexPage2, countof(kComplexPage2));
}
const TextAndModel kSubtleCrown[] = {
@ -483,11 +483,11 @@ const TextAndModel kSubtleCrown[] = {
};
TEST(ParagraphsTest, TestSubtleCrown) {
TestParagraphDetection(kSubtleCrown, ABSL_ARRAYSIZE(kSubtleCrown) - 1);
TestParagraphDetection(kSubtleCrown, countof(kSubtleCrown) - 1);
}
TEST(ParagraphsTest, TestStrayLineInBlock) {
TestParagraphDetection(kSubtleCrown, ABSL_ARRAYSIZE(kSubtleCrown));
TestParagraphDetection(kSubtleCrown, countof(kSubtleCrown));
}
const TextAndModel kUnlvRep3AO[] = {
@ -531,7 +531,7 @@ const TextAndModel kUnlvRep3AO[] = {
};
TEST(ParagraphsTest, TestUnlvInsurance) {
TestParagraphDetection(kUnlvRep3AO, ABSL_ARRAYSIZE(kUnlvRep3AO));
TestParagraphDetection(kUnlvRep3AO, countof(kUnlvRep3AO));
}
// The basic outcome we want for something with a bunch of leader dots is that
@ -556,7 +556,7 @@ const TextAndModel kTableOfContents[] = {
};
TEST(ParagraphsTest, TestSplitsOutLeaderLines) {
TestParagraphDetection(kTableOfContents, ABSL_ARRAYSIZE(kTableOfContents));
TestParagraphDetection(kTableOfContents, countof(kTableOfContents));
}
const TextAndModel kTextWithSourceCode[] = {
@ -592,7 +592,7 @@ const TextAndModel kTextWithSourceCode[] = {
TEST(ParagraphsTest, NotDistractedBySourceCode) {
TestParagraphDetection(kTextWithSourceCode,
ABSL_ARRAYSIZE(kTextWithSourceCode));
countof(kTextWithSourceCode));
}
const TextAndModel kOldManAndSea[] = {
@ -659,7 +659,7 @@ const TextAndModel kOldManAndSea[] = {
};
TEST(ParagraphsTest, NotOverlyAggressiveWithBlockQuotes) {
TestParagraphDetection(kOldManAndSea, ABSL_ARRAYSIZE(kOldManAndSea));
TestParagraphDetection(kOldManAndSea, countof(kOldManAndSea));
}
const TextAndModel kNewZealandIndex[] = {
@ -697,7 +697,7 @@ const TextAndModel kNewZealandIndex[] = {
};
TEST(ParagraphsTest, IndexPageTest) {
TestParagraphDetection(kNewZealandIndex, ABSL_ARRAYSIZE(kNewZealandIndex));
TestParagraphDetection(kNewZealandIndex, countof(kNewZealandIndex));
}
// TODO(eger): Add some right-to-left examples, and fix the algorithm as needed.

View File

@ -486,12 +486,12 @@ TEST_F(ResultIteratorTest, DualStartTextlineOrderTest) {
7, 6,
5, ResultIterator::kMinorRunEnd};
ExpectTextlineReadingOrder(true, word_dirs, ABSL_ARRAYSIZE(word_dirs),
ExpectTextlineReadingOrder(true, word_dirs, countof(word_dirs),
reading_order_ltr_context,
ABSL_ARRAYSIZE(reading_order_ltr_context));
ExpectTextlineReadingOrder(false, word_dirs, ABSL_ARRAYSIZE(word_dirs),
countof(reading_order_ltr_context));
ExpectTextlineReadingOrder(false, word_dirs, countof(word_dirs),
reading_order_rtl_context,
ABSL_ARRAYSIZE(reading_order_rtl_context));
countof(reading_order_rtl_context));
}
// Tests that clearly left-direction text (with no right-to-left indications)
@ -506,12 +506,12 @@ TEST_F(ResultIteratorTest, LeftwardTextlineOrderTest) {
ResultIterator::kMinorRunStart, 0, 1, 2, 3, 4, 5, 6, 7,
ResultIterator::kMinorRunEnd};
ExpectTextlineReadingOrder(true, word_dirs, ABSL_ARRAYSIZE(word_dirs),
ExpectTextlineReadingOrder(true, word_dirs, countof(word_dirs),
reading_order_ltr_context,
ABSL_ARRAYSIZE(reading_order_ltr_context));
ExpectTextlineReadingOrder(false, word_dirs, ABSL_ARRAYSIZE(word_dirs),
countof(reading_order_ltr_context));
ExpectTextlineReadingOrder(false, word_dirs, countof(word_dirs),
reading_order_rtl_context,
ABSL_ARRAYSIZE(reading_order_rtl_context));
countof(reading_order_rtl_context));
}
// Test that right-direction text comes out strictly right-to-left in
@ -520,9 +520,9 @@ TEST_F(ResultIteratorTest, RightwardTextlineOrderTest) {
const StrongScriptDirection word_dirs[] = {dR, dR, dN, dR, dN, dN, dR, dR};
// The order here is just right-to-left, nothing fancy.
int reading_order_rtl_context[] = {7, 6, 5, 4, 3, 2, 1, 0};
ExpectTextlineReadingOrder(false, word_dirs, ABSL_ARRAYSIZE(word_dirs),
ExpectTextlineReadingOrder(false, word_dirs, countof(word_dirs),
reading_order_rtl_context,
ABSL_ARRAYSIZE(reading_order_rtl_context));
countof(reading_order_rtl_context));
}
TEST_F(ResultIteratorTest, TextlineOrderSanityCheck) {

View File

@ -24,7 +24,7 @@ class STATSTest : public testing::Test {
void SetUp() {
std::locale::global(std::locale(""));
stats_.set_range(0, 16);
for (size_t i = 0; i < ARRAYSIZE(kTestData); ++i)
for (size_t i = 0; i < countof(kTestData); ++i)
stats_.add(i, kTestData[i]);
}