File indexing completed on 2025-01-05 03:58:02

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-06-29
0007  * Description : Test the functions for dealing with DatabaseFields
0008  *
0009  * SPDX-FileCopyrightText: 2013 by Michael G. Hansen <mike at mghansen dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "databasefields_utest.h"
0016 
0017 // Qt includes
0018 
0019 
0020 // Local includes
0021 
0022 #include "coredbfields.h"
0023 
0024 using namespace Digikam;
0025 
0026 QTEST_GUILESS_MAIN(DatabaseFieldsTest)
0027 
0028 using namespace DatabaseFields;
0029 
0030 void DatabaseFieldsTest::cleanupTestCase()
0031 {
0032 }
0033 
0034 void DatabaseFieldsTest::initTestCase()
0035 {
0036 }
0037 
0038 #define DECLARE_ITERATOR_TEST(Field)                    \
0039 {                                                       \
0040     Field i;                                            \
0041     for (Field##Iterator it ; !it.atEnd() ; ++it)       \
0042     {                                                   \
0043         i |= *it;                                       \
0044     }                                                   \
0045     QCOMPARE(i, Field##All);                            \
0046 }
0047 
0048 void DatabaseFieldsTest::testIterators()
0049 {
0050     // test that the iterator iterates over all fields
0051 
0052     DECLARE_ITERATOR_TEST(Images)
0053     DECLARE_ITERATOR_TEST(ItemInformation)
0054     DECLARE_ITERATOR_TEST(ImageMetadata)
0055     DECLARE_ITERATOR_TEST(VideoMetadata)
0056     DECLARE_ITERATOR_TEST(ItemPositions)
0057     DECLARE_ITERATOR_TEST(ItemComments)
0058     DECLARE_ITERATOR_TEST(ImageHistoryInfo)
0059 }
0060 
0061 void DatabaseFieldsTest::testMetaInfo()
0062 {
0063 
0064 // Do not compile due to undefined reference to `Digikam::DatabaseFields::FieldMetaInfo<QFlags<Digikam::DatabaseFields::ImagesField> >::Last'
0065 // Happens even though file defining those libs/database/framework/databasefields.h is included and linking against digikamgui.
0066 
0067 /*
0068     QCOMPARE(FieldMetaInfo<Images>::Last, ImagesLast);
0069     QCOMPARE(FieldMetaInfo<ItemInformation>::Last, ItemInformationLast);
0070     QCOMPARE(FieldMetaInfo<ImageMetadata>::Last, ImageMetadataLast);
0071     QCOMPARE(FieldMetaInfo<ItemComments>::Last, ItemCommentsLast);
0072     QCOMPARE(FieldMetaInfo<ItemPositions>::Last, ItemPositionsLast);
0073     QCOMPARE(FieldMetaInfo<ImageHistoryInfo>::Last, ImageHistoryInfoLast);
0074     QCOMPARE(FieldMetaInfo<VideoMetadata>::Last, VideoMetadataLast);
0075 */
0076 }
0077 
0078 #define DECLARE_ITERATORSETONLY_TEST(Field)                                                 \
0079     for (Field i = Field##None ; i<=Field##All ; i=Field(int(i) + 1))                       \
0080     {                                                                                       \
0081         Field i2 = Field##None;                                                             \
0082         for (DatabaseFieldsEnumIteratorSetOnly<Field> iOnly(i) ; !iOnly.atEnd() ; ++iOnly)  \
0083         {                                                                                   \
0084             i2 |= *iOnly;                                                                   \
0085         }                                                                                   \
0086         QCOMPARE(i, i2);                                                                    \
0087     }
0088 
0089 void DatabaseFieldsTest::testIteratorsSetOnly()
0090 {
0091     DECLARE_ITERATORSETONLY_TEST(Images)
0092     DECLARE_ITERATORSETONLY_TEST(ItemInformation)
0093     DECLARE_ITERATORSETONLY_TEST(ImageMetadata)
0094     DECLARE_ITERATORSETONLY_TEST(VideoMetadata)
0095     DECLARE_ITERATORSETONLY_TEST(ItemPositions)
0096     DECLARE_ITERATORSETONLY_TEST(ItemComments)
0097     DECLARE_ITERATORSETONLY_TEST(ImageHistoryInfo)
0098 }
0099 
0100 void DatabaseFieldsTest::testSet()
0101 {
0102     QCOMPARE(Set(ImagesFirst).getImages(), ImagesFirst);
0103     QCOMPARE(Set(ItemInformationFirst).getItemInformation(), ItemInformationFirst);
0104     QCOMPARE(Set(ImageMetadataFirst).getImageMetadata(), ImageMetadataFirst);
0105     QCOMPARE(Set(VideoMetadataFirst).getVideoMetadata(), VideoMetadataFirst);
0106     QCOMPARE(Set(ItemPositionsFirst).getItemPositions(), ItemPositionsFirst);
0107     QCOMPARE(Set(ItemCommentsFirst).getItemComments(), ItemCommentsFirst);
0108     QCOMPARE(Set(ImageHistoryInfoFirst).getImageHistoryInfo(), ImageHistoryInfoFirst);
0109 }
0110 
0111 typedef Hash<Set> SetHash;
0112 
0113 SetHash SetHashAddSets(const SetHash& targetIn, const Set& bits)
0114 {
0115     SetHash target(targetIn);
0116 
0117     for (DatabaseFieldsEnumIteratorSetOnly<Images> it(bits.getImages()) ; !it.atEnd() ; ++it)
0118     {
0119         target.insertField(*it, Set(*it));
0120     }
0121 
0122     for (DatabaseFieldsEnumIteratorSetOnly<ItemInformation> it(bits.getItemInformation()) ; !it.atEnd() ; ++it)
0123     {
0124         target.insertField(*it, Set(*it));
0125     }
0126 
0127     for (DatabaseFieldsEnumIteratorSetOnly<ImageMetadata> it(bits.getImageMetadata()) ; !it.atEnd() ; ++it)
0128     {
0129         target.insertField(*it, Set(*it));
0130     }
0131 
0132     for (DatabaseFieldsEnumIteratorSetOnly<VideoMetadata> it(bits.getVideoMetadata()) ; !it.atEnd() ; ++it)
0133     {
0134         target.insertField(*it, Set(*it));
0135     }
0136 
0137     for (DatabaseFieldsEnumIteratorSetOnly<ItemComments> it(bits.getItemComments()) ; !it.atEnd() ; ++it)
0138     {
0139         target.insertField(*it, Set(*it));
0140     }
0141 
0142     for (DatabaseFieldsEnumIteratorSetOnly<ItemPositions> it(bits.getItemPositions()) ; !it.atEnd() ; ++it)
0143     {
0144         target.insertField(*it, Set(*it));
0145     }
0146 
0147     for (DatabaseFieldsEnumIteratorSetOnly<ImageHistoryInfo> it(bits.getImageHistoryInfo()) ; !it.atEnd() ; ++it)
0148     {
0149         target.insertField(*it, Set(*it));
0150     }
0151 
0152     return target;
0153 }
0154 
0155 void DatabaseFieldsTest::testSetHashAddSets()
0156 {
0157     SetHash t;
0158     int itemCount = 0;
0159 
0160     QVERIFY(t.isEmpty());
0161     t = SetHashAddSets(t, DatabaseFields::Set(ImagesFirst));
0162     ++itemCount;
0163     QCOMPARE(t.size(), itemCount);
0164     QCOMPARE(t.value(ImagesFirst).getImages(), ImagesFirst);
0165 
0166     t = SetHashAddSets(t, DatabaseFields::Set(ImagesLast));
0167     ++itemCount;
0168     QCOMPARE(t.size(), itemCount);
0169     QCOMPARE(t.value(ImagesFirst).getImages(), ImagesFirst);
0170 
0171     // test insertion of or`ed values
0172 
0173     t = SetHashAddSets(t, DatabaseFields::Set(ItemInformationFirst | ItemInformationLast));
0174     itemCount+=2;
0175     QCOMPARE(t.size(), itemCount);
0176     QCOMPARE(t.value(ItemInformationFirst).getItemInformation(), ItemInformationFirst);
0177     QCOMPARE(t.value(ItemInformationLast).getItemInformation(), ItemInformationLast);
0178 
0179     t = SetHashAddSets(t, DatabaseFields::Set(ImageMetadataFirst));
0180     ++itemCount;
0181     QCOMPARE(t.size(), itemCount);
0182     QCOMPARE(t.value(ImageMetadataFirst).getImageMetadata(), ImageMetadataFirst);
0183 
0184     t = SetHashAddSets(t, DatabaseFields::Set(VideoMetadataFirst));
0185     ++itemCount;
0186     QCOMPARE(t.size(), itemCount);
0187     QCOMPARE(t.value(VideoMetadataFirst).getVideoMetadata(), VideoMetadataFirst);
0188 
0189     t = SetHashAddSets(t, DatabaseFields::Set(ItemCommentsFirst));
0190     ++itemCount;
0191     QCOMPARE(t.size(), itemCount);
0192     QCOMPARE(t.value(ItemCommentsFirst).getItemComments(), ItemCommentsFirst);
0193 
0194     t = SetHashAddSets(t, DatabaseFields::Set(ItemPositionsFirst));
0195     ++itemCount;
0196     QCOMPARE(t.size(), itemCount);
0197     QCOMPARE(t.value(ItemPositionsFirst).getItemPositions(), ItemPositionsFirst);
0198 
0199     t = SetHashAddSets(t, DatabaseFields::Set(ImageHistoryInfoFirst));
0200     ++itemCount;
0201     QCOMPARE(t.size(), itemCount);
0202     QCOMPARE(t.value(ImageHistoryInfoFirst).getImageHistoryInfo(), ImageHistoryInfoFirst);
0203 }
0204 
0205 
0206 void DatabaseFieldsTest::testHashRemoveAll()
0207 {
0208     Set setToAdd =
0209                 Set(DatabaseFields::Set(ImagesFirst | ImagesLast))
0210                 .setFields(DatabaseFields::Set(ItemInformationFirst))
0211                 .setFields(DatabaseFields::Set(ImageMetadataFirst))
0212                 .setFields(DatabaseFields::Set(VideoMetadataFirst))
0213                 .setFields(DatabaseFields::Set(ItemCommentsFirst))
0214                 .setFields(DatabaseFields::Set(ItemPositionsFirst | ItemPositionsLast))
0215                 .setFields(DatabaseFields::Set(ImageHistoryInfoFirst));
0216 
0217     SetHash t;
0218     t = SetHashAddSets(t, setToAdd);
0219 
0220     const int c1 = t.size();
0221 
0222     // test regular remove
0223 
0224     SetHash t2(t);
0225     QCOMPARE(t2.remove(ImagesFirst), 1);
0226     QCOMPARE(t2.size(), c1-1);
0227 
0228     // test removeAllFields: First and Last are in the hash, None is not --> 2 entries should be removed
0229 
0230     SetHash t3(t);
0231     QCOMPARE(t3.removeAllFields(ImagesFirst | ImagesLast | ImagesNone), 2);
0232     QCOMPARE(t3.size(), c1-2);
0233 }
0234 
0235 #define DECLARE_MINSIZE_TEST(FieldName)                                     \
0236     {                                                                       \
0237         const FieldMetaInfo<FieldName>::MinSizeType m = FieldName##All;     \
0238         const FieldName v = FieldMetaInfo<FieldName>::fromMinSizeType(m);   \
0239         QCOMPARE(v, FieldName##All);                                        \
0240     }
0241 
0242 /**
0243  * Verify that the minimum size types still hold all info of the enum.
0244  */
0245 void DatabaseFieldsTest::testMinSizeType()
0246 {
0247     DECLARE_MINSIZE_TEST(Images)
0248     DECLARE_MINSIZE_TEST(ItemInformation)
0249     DECLARE_MINSIZE_TEST(ImageMetadata)
0250     DECLARE_MINSIZE_TEST(ItemComments)
0251     DECLARE_MINSIZE_TEST(ItemPositions)
0252     DECLARE_MINSIZE_TEST(ImageHistoryInfo)
0253     DECLARE_MINSIZE_TEST(VideoMetadata)
0254 }
0255 
0256 #include "moc_databasefields_utest.cpp"