Exemple #1
0
void assert_ValueFuntion_isSymmetrical(ValueFunction *vfunc) {
#if STATE_SPACE_DIMENSION != 4
#error "Works only in the four dimensional state space"
#endif

	if (vfunc == NULL)
		return;

	GameStateDomain *domain = vfunc->getDomain();
	GridPointIndex *maxIndex = domain->getMaximalGridIndex();
	GridPointIndex *cur = new int[STATE_SPACE_DIMENSION];
	GridPointIndex *sym = new int[STATE_SPACE_DIMENSION];

	for (int i0 = 0; i0 < maxIndex[0] + 1; i0++) {
		cur[0] = i0;
		for (int i1 = 0; i1 < maxIndex[1] + 1; i1++) {
			cur[1] = i1;
			for (int i2 = 0; i2 < maxIndex[2] + 1; i2++) {
				cur[2] = i2;
				for (int i3 = 0; i3 < maxIndex[3] + 1; i3++) {
					cur[3] = i3;

					/* Now e check all the symmtries of the value function by
					 * Cristini and Falcone mentioned on page 194 (or 18)
					 */

					// First build symmetry (20)
					for (int i = 0; i < STATE_SPACE_DIMENSION; i++)
						sym[i] = maxIndex[i] - cur[i];

					ASSERT_EQUALS(vfunc->getValue(cur), vfunc->getValue(sym),
							1e-3,
							"No Symmetry (20) at %d %d %d %d (%f) != %d %d %d %d (%f)\n",
							cur[0], cur[1], cur[2], cur[3], vfunc->getValue(cur), sym[0], sym[1], sym[2], sym[3], vfunc->getValue(sym));

					// Next Symmetry (21)
					sym[0] = maxIndex[0] - cur[0];
					sym[1] = cur[1];
					sym[2] = maxIndex[2] - cur[2];
					sym[3] = cur[3];
					ASSERT_EQUALS(vfunc->getValue(cur), vfunc->getValue(sym),
							1e-3,
							"No Symmetry (21) at %d %d %d %d (%f) != %d %d %d %d (%f)\n",
							cur[0], cur[1], cur[2], cur[3], vfunc->getValue(cur), sym[0], sym[1], sym[2], sym[3], vfunc->getValue(sym));

					// Next Symmetry (22)
					sym[0] = cur[0];
					sym[1] = maxIndex[1] - cur[1];
					sym[2] = cur[2];
					sym[3] = maxIndex[3] - cur[3];
					ASSERT_EQUALS(vfunc->getValue(cur), vfunc->getValue(sym),
							1e-3,
							"No Symmetry (22) at %d %d %d %d (%f) != %d %d %d %d (%f)\n",
							cur[0], cur[1], cur[2], cur[3], vfunc->getValue(cur), sym[0], sym[1], sym[2], sym[3], vfunc->getValue(sym));
				}
			}
		}
	}

	delete[] cur;
}
Exemple #2
0
TEST_F(SyncTailTest, MultiSyncApplyGroupsInsertOperationByNamespaceBeforeApplying) {
    int seconds = 0;
    auto makeOp = [&seconds](const NamespaceString& nss) {
        return makeInsertDocumentOplogEntry(
            {Timestamp(Seconds(seconds), 0), 1LL}, nss, BSON("_id" << seconds++));
    };
    NamespaceString nss1("test." + _agent.getSuiteName() + "_" + _agent.getTestName() + "_1");
    NamespaceString nss2("test." + _agent.getSuiteName() + "_" + _agent.getTestName() + "_2");
    auto createOp1 = makeCreateCollectionOplogEntry({Timestamp(Seconds(seconds++), 0), 1LL}, nss1);
    auto createOp2 = makeCreateCollectionOplogEntry({Timestamp(Seconds(seconds++), 0), 1LL}, nss2);
    auto insertOp1a = makeOp(nss1);
    auto insertOp1b = makeOp(nss1);
    auto insertOp2a = makeOp(nss2);
    auto insertOp2b = makeOp(nss2);
    MultiApplier::Operations operationsApplied;
    auto syncApply = [&operationsApplied](OperationContext*, const BSONObj& op, bool) {
        operationsApplied.push_back(OplogEntry(op));
        return Status::OK();
    };

    MultiApplier::OperationPtrs ops = {
        &createOp1, &createOp2, &insertOp1a, &insertOp2a, &insertOp1b, &insertOp2b};
    ASSERT_OK(multiSyncApply_noAbort(_txn.get(), &ops, syncApply));

    ASSERT_EQUALS(4U, operationsApplied.size());
    ASSERT_EQUALS(createOp1, operationsApplied[0]);
    ASSERT_EQUALS(createOp2, operationsApplied[1]);

    // Check grouped insert operations in namespace "nss1".
    ASSERT_EQUALS(insertOp1a.getOpTime(), operationsApplied[2].getOpTime());
    ASSERT_EQUALS(insertOp1a.ns, operationsApplied[2].ns);
    ASSERT_EQUALS(BSONType::Array, operationsApplied[2].o.type());
    auto group1 = operationsApplied[2].o.Array();
    ASSERT_EQUALS(2U, group1.size());
    ASSERT_EQUALS(insertOp1a.o.Obj(), group1[0].Obj());
    ASSERT_EQUALS(insertOp1b.o.Obj(), group1[1].Obj());

    // Check grouped insert operations in namespace "nss2".
    ASSERT_EQUALS(insertOp2a.getOpTime(), operationsApplied[3].getOpTime());
    ASSERT_EQUALS(insertOp2a.ns, operationsApplied[3].ns);
    ASSERT_EQUALS(BSONType::Array, operationsApplied[3].o.type());
    auto group2 = operationsApplied[3].o.Array();
    ASSERT_EQUALS(2U, group2.size());
    ASSERT_EQUALS(insertOp2a.o.Obj(), group2[0].Obj());
    ASSERT_EQUALS(insertOp2b.o.Obj(), group2[1].Obj());
}
void CTestAsyncSuplLbsApi::TestObserverLocalL()
//
// Test the use of an MLbsHostSettingsStoreObserver implementation within
// the process that is modifying the settings store.
//
// This test creates two instances of CLbsHostSettingsStore and an observer
// instance for each one. Only the first store instance is used for settings
// modifications. The API specifies that an application will not receive
// notifications for changes it itself has made via the same instance of
// CLbsHostSettingsStore, so only observer 2 should receive notifications.
//
// 1. Create two observers
// 2. Create two store instances and start notifiers
// 3. Create waiter
// 4. Read the initial host settings in from the ini file
// 5. Create a host settings entry using the initial settings
// 6. Retrieve & validate
// 7. Wait for notifier to call LbsHostSettingsUpdated()
// 8. Get new host settings
// 9. Update the previously added entry
// 10. Retrieve and validate
// 11. Wait for notifier to call LbsHostSettingsUpdated()
// 12. Create another settings entry
// 13. Wait for notifier to call LbsHostSettingsUpdated()
// 14. Set second entry as default
// 15. Update second entry
// 16. Wait for notifier to call LbsHostSettingsUpdated()
// 17. Delete first entry
// 18. Wait for notifier to call LbsHostSettingsUpdated()
// 19. Try to update a non-existent entry
// 20. Update a valid entry
// 21. Wait for notifier to call LbsHostSettingsUpdated()
// 22. Try to delete a non-existent entry
// 23. Delete a valid entry
// 24. Wait for notifier to call LbsHostSettingsUpdated()
// 25. Retrieve the extended interface
// 26. Check the version number
//
// Note that for a given observer, the calls to WaitActive() catch only the
// *first* notification since the previous call to that function. Any others
// will be lost since the CenRep notification request is not renewed until
// CLbsHostSettingsNotifier::RunL() gets invoked. This should not matter since
// we expect only one notification per modified settings entry, but it doesn't
// prove that we aren't getting multiple notifications. (TestObserverMultiAppL()
// does not suffer from this limitation.)
//
	{
	INFO_PRINTF1(_L("CTestAsyncSuplLbsApi::TestObserverLocalL"));
	
	// Create active scheduler
	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
	CleanupStack::PushL(scheduler); 
	CActiveScheduler::Install(scheduler);

	// 1. Create two observers
	iObserver1 = CTestLbsHostSettingsStoreObserver::NewL(this);
	iObserver2 = CTestLbsHostSettingsStoreObserver::NewL(this);

	// 2. Create two store instances with the corresponding observers,
	// and start notifiers
	iLBSHostStore1 = CLbsHostSettingsStore::NewL(KLbsHostSettingsSuplStoreId, *iObserver1);
	iLBSHostStore2 = CLbsHostSettingsStore::NewL(KLbsHostSettingsSuplStoreId, *iObserver2);

	// 3. Create waiter. Note: waiter must be created after notifiers are
	// started otherwise a panic E32USER-CBase 46 (stray signal) results.
    iWaiter = new(ELeave) CActiveWaiter;

	// 4. Read the initial host settings in from the ini file
	TLbsHostSettingsSupl settings;
	ASSERT_EQUALS(KErrNone, CTestSuplUtils::GetHostSettingFromConfig(iConfig, settings, _L("HostSetting_Default")));
			
	// 5. Create a host settings entry using the initial settings
	TLbsHostSettingsId newSettingId1;
	TInt err = iLBSHostStore1->CreateHostSettings(settings, KLbsHostSettingsTestCreatorId, newSettingId1);	
	ASSERT_EQUALS(KErrNone, err);
	
	// 6. Retrieve & validate
	TLbsHostSettingsSupl settingResult;
	err = iLBSHostStore1->GetHostSettings(newSettingId1, settingResult);	
	ASSERT_EQUALS(err, KErrNone);
	ASSERT_TRUE(CTestSuplUtils::AreSettingsEqual(settings, settingResult));

	// 7. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId1);
	ASSERT_FALSE(iObserver1Notified);

	// 8. Get new host settings
	TLbsHostSettingsSupl settings2;
	ASSERT_EQUALS(KErrNone, CTestSuplUtils::GetHostSettingFromConfig(iConfig, settings2, _L("HostSetting_Two")));
	
	// 9. Update the previously added entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId1, settings2);	
	ASSERT_EQUALS(err, KErrNone);
	
	// 10. Retrieve and validate
	err = iLBSHostStore1->GetHostSettings(newSettingId1, settingResult);	
	ASSERT_EQUALS(err, KErrNone);
	ASSERT_TRUE(CTestSuplUtils::AreSettingsEqual(settings2, settingResult));

	// 11. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId1);
	ASSERT_FALSE(iObserver1Notified);

	// 12. Create another settings entry
	TLbsHostSettingsId newSettingId2;
	err = iLBSHostStore1->CreateHostSettings(settings, KLbsHostSettingsTestCreatorId, newSettingId2);	
	ASSERT_EQUALS(KErrNone, err);

	// 13. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId2);
	ASSERT_FALSE(iObserver1Notified);

	// 14. Set second entry as default. This should not result in any
	// notification since the default host key is part of the metadata space.
	// (If it did then the test under step 16 below would fail since
	// LbsHostSettingsUpdated() would be called with settings
	// ID = 2 [EDefaultHost] but newSettingsId2 = 1.)
	err = iLBSHostStore1->SetDefaultHostSettings(newSettingId2);	
	ASSERT_EQUALS(err, KErrNone);

	// 15. Update second entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId2, settings2);	
	ASSERT_EQUALS(err, KErrNone);

	// 16. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId2);
	ASSERT_FALSE(iObserver1Notified);

	// 17. Delete first entry
	err = iLBSHostStore1->DeleteHostSettings(newSettingId1);
	ASSERT_EQUALS(err, KErrNone);

	// 18. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId1);
	ASSERT_FALSE(iObserver1Notified);

	// 19. Try to update a non-existent entry. This shouldn't result in any
	// notification. (If it did then the test under step 21 below would
	// fail since LbsHostSettingsUpdated() would be called with settings
	// ID = newSettingsId1.)
	err = iLBSHostStore1->UpdateHostSettings(newSettingId1, settings);	
	ASSERT_EQUALS(err, KErrNotFound);

	// 20. Update a valid entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId2, settings);	
	ASSERT_EQUALS(err, KErrNone);

	// 21. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId2);
	ASSERT_FALSE(iObserver1Notified);

	// 22. Try to delete a non-existent entry. This shouldn't result in any
	// notification. (If it did then the test under step 24 below would
	// fail since LbsHostSettingsUpdated() would be called with settings
	// ID = newSettingsId1.)
	err = iLBSHostStore1->DeleteHostSettings(newSettingId1);	
	ASSERT_EQUALS(err, KErrNotFound);

	// 23. Delete a valid entry
	err = iLBSHostStore1->DeleteHostSettings(newSettingId2);
	ASSERT_EQUALS(err, KErrNone);

	// 24. Wait for notifier to call LbsHostSettingsUpdated()
    iWaiter->WaitActive();
	ASSERT_EQUALS(iObservedError, KErrNone);
	ASSERT_EQUALS(iObservedStoreId, KLbsHostSettingsSuplStoreId);
	ASSERT_EQUALS(iObservedSettingsId, newSettingId2);
	ASSERT_FALSE(iObserver1Notified);

	// 25. Retrieve the extended interface
	TAny* ei = iObserver1->ExtendedInterface(0, NULL, NULL);
	ASSERT_TRUE(ei == NULL);
	
	// 26. Check the version number
	TVersion ver = iObserver1->Version();
	ASSERT_TRUE(
			(ver.iMajor == KObserverVersion.iMajor) &&
			(ver.iMinor == KObserverVersion.iMinor) &&
			(ver.iBuild == KObserverVersion.iBuild)
	);
	CleanupStack::PopAndDestroy(scheduler);	
	}
    void suppressionsGlob() {
        // Check for syntax errors in glob
        {
            Suppressions suppressions;
            std::istringstream s("errorid:**.cpp\n");
            ASSERT_EQUALS("Failed to add suppression. Syntax error in glob.", suppressions.parseFile(s));
        }

        // Check that globbing works
        {
            Suppressions suppressions;
            std::istringstream s("errorid:x*.cpp\nerrorid:y?.cpp\nerrorid:test.c*");
            ASSERT_EQUALS("", suppressions.parseFile(s));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "xyz.cpp", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "xyz.cpp.cpp", 1));
            ASSERT_EQUALS(false, suppressions.isSuppressed("errorid", "abc.cpp", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "ya.cpp", 1));
            ASSERT_EQUALS(false, suppressions.isSuppressed("errorid", "y.cpp", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "test.c", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "test.cpp", 1));
        }

        // Check that both a filename match and a glob match apply
        {
            Suppressions suppressions;
            std::istringstream s("errorid:x*.cpp\nerrorid:xyz.cpp:1\nerrorid:a*.cpp:1\nerrorid:abc.cpp:2");
            ASSERT_EQUALS("", suppressions.parseFile(s));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "xyz.cpp", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "xyz.cpp", 2));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "abc.cpp", 1));
            ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "abc.cpp", 2));
        }
    }
Exemple #5
0
TEST_F(SyncTailTest, MultiApplyReturnsBadValueOnNullWriterPool) {
    auto op = makeCreateCollectionOplogEntry({Timestamp(Seconds(1), 0), 1LL});
    auto status = multiApply(_txn.get(), nullptr, {op}, noopApplyOperationFn).getStatus();
    ASSERT_EQUALS(ErrorCodes::BadValue, status);
    ASSERT_STRING_CONTAINS(status.reason(), "invalid worker pool");
}
Exemple #6
0
 void checkValid( int nKeys ) const {
     ASSERT( bt() );
     ASSERT( bt()->isHead() );
     bt()->assertValid( order(), true );
     ASSERT_EQUALS( nKeys, bt()->fullValidate( dl(), order() ) );
 }
    void runChecks(void (TestSuppressions::*check)(const char[], const std::string &)) {
        // check to make sure the appropriate error is present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    a++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());

        // suppress uninitvar globally
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    a++;\n"
                       "}\n",
                       "uninitvar");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar globally, without error present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    b++;\n"
                       "}\n",
                       "uninitvar");
        ASSERT_EQUALS("[*]: (information) Unmatched suppression: uninitvar\n", errout.str());

        // suppress uninitvar for this file only
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    a++;\n"
                       "}\n",
                       "uninitvar:test.cpp");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar for this file only, without error present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    b++;\n"
                       "}\n",
                       "uninitvar:test.cpp");
        ASSERT_EQUALS("[test.cpp]: (information) Unmatched suppression: uninitvar\n", errout.str());

        // suppress all for this file only
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    a++;\n"
                       "}\n",
                       "*:test.cpp");
        ASSERT_EQUALS("", errout.str());

        // suppress all for this file only, without error present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    b++;\n"
                       "}\n",
                       "*:test.cpp");
        ASSERT_EQUALS("[test.cpp]: (information) Unmatched suppression: *\n", errout.str());

        // suppress uninitvar for this file and line
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    a++;\n"
                       "}\n",
                       "uninitvar:test.cpp:3");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar for this file and line, without error present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    b++;\n"
                       "}\n",
                       "uninitvar:test.cpp:3");
        ASSERT_EQUALS("[test.cpp:3]: (information) Unmatched suppression: uninitvar\n", errout.str());

        // suppress uninitvar inline
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    // cppcheck-suppress uninitvar\n"
                       "    a++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar inline
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    // cppcheck-suppress uninitvar\n"
                       "\n"
                       "    a++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar inline
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    /* cppcheck-suppress uninitvar */\n"
                       "    a++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar inline
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    /* cppcheck-suppress uninitvar */\n"
                       "\n"
                       "    a++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("", errout.str());

        // suppress uninitvar inline, without error present
        (this->*check)("void f() {\n"
                       "    int a;\n"
                       "    // cppcheck-suppress uninitvar\n"
                       "    b++;\n"
                       "}\n",
                       "");
        ASSERT_EQUALS("[test.cpp:4]: (information) Unmatched suppression: uninitvar\n", errout.str());
    }
Exemple #8
0
 TEST( LexNumCmp, Simple1 ) {
     ASSERT_EQUALS( 0, LexNumCmp::cmp( "a.b.c", "a.b.c", false ) );
 }
Exemple #9
0
    void wrongMode_simple() {
        // Read mode
        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"r\");\n"
              "    fread(buffer, 5, 6, f);\n"
              "    rewind(f);\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Write operation on a file that was opened only for reading.\n", errout.str());

        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"r+\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Write mode
        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"w\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    rewind(f);\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());

        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"w+\");\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Append mode
        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"a\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    rewind(f);\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());

        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"a+\");\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Variable declared locally
        check("void foo() {\n"
              "    FILE* f = fopen(name, \"r\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    fclose(f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was opened only for reading.\n", errout.str());

        // Call unknown function
        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"a\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    bar(f);\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"a\");\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    clearerr(f);\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());

        // freopen and tmpfile
        check("void foo(FILE*& f) {\n"
              "    f = freopen(name, \"r\", f);\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was opened only for reading.\n", errout.str());

        // Crash tests
        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, mode);\n" // No assertion failure (#3830)
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void fopen(std::string const &filepath, std::string const &mode);"); // #3832
    }
Exemple #10
0
 void run() {
     insert( "{\"a\":\"b\"}" );
     insert( "{\"a\":\"b\",\"x\":\"y\"}" );
     insert( "{\"a\":\"c\"}" );
     ASSERT_EQUALS(2ULL, _client.count(ns(), fromjson("{\"a\":\"b\"}")));
 }
Exemple #11
0
 void run() {
     insert( "{\"a\":\"c\"}" );
     insert( "{\"a\":\"b\"}" );
     insert( "{\"a\":\"d\"}" );
     ASSERT_EQUALS(1ULL, _client.count(ns(), fromjson("{\"a\":/^b/}")));
 }
    void returnReference2() {
        check("class Fred {\n"
              "    std::string &foo();\n"
              "}\n"
              "std::string &Fred::foo()\n"
              "{\n"
              "    std::string s;\n"
              "    return s;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:7]: (error) Reference to auto variable returned.\n", errout.str());

        check("class Fred {\n"
              "    std::vector<int> &foo();\n"
              "};\n"
              "std::vector<int> &Fred::foo()\n"
              "{\n"
              "    std::vector<int> v;\n"
              "    return v;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:7]: (error) Reference to auto variable returned.\n", errout.str());

        check("class Fred {\n"
              "    std::vector<int> &foo();\n"
              "};\n"
              "std::vector<int> &Fred::foo()\n"
              "{\n"
              "    static std::vector<int> v;\n"
              "    return v;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("class Fred {\n"
              "    std::string &f();\n"
              "};\n"
              "std::string hello()\n"
              "{\n"
              "     return \"hello\";\n"
              "}\n"
              "std::string &Fred::f()\n"
              "{\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:10]: (error) Reference to temporary returned.\n", errout.str());

        check("class Fred {\n"
              "    std::string hello();\n"
              "    std::string &f();\n"
              "};\n"
              "std::string Fred::hello()\n"
              "{\n"
              "     return \"hello\";\n"
              "}\n"
              "std::string &Fred::f()\n"
              "{\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:11]: (error) Reference to temporary returned.\n", errout.str());

        check("class Bar;\n"
              "Bar foo() {\n"
              "     return something;\n"
              "}\n"
              "Bar& bar() {\n"
              "    return foo();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:6]: (error) Reference to temporary returned.\n", errout.str());

        check("std::map<int, string> foo() {\n"
              "     return something;\n"
              "}\n"
              "std::map<int, string>& bar() {\n"
              "    return foo();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Reference to temporary returned.\n", errout.str());

        check("Bar foo() {\n"
              "     return something;\n"
              "}\n"
              "Bar& bar() {\n" // Unknown type - might be a typedef to a reference type
              "    return foo();\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Don't crash with function in unknown scope (#4076)
        check("X& a::Bar() {}"
              "X& foo() {"
              "    return Bar();"
              "}");
    }
    void returnReference1() {
        check("std::string &foo()\n"
              "{\n"
              "    std::string s;\n"
              "    return s;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Reference to auto variable returned.\n", errout.str());

        check("std::vector<int> &foo()\n"
              "{\n"
              "    std::vector<int> v;\n"
              "    return v;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Reference to auto variable returned.\n", errout.str());

        check("std::vector<int> &foo()\n"
              "{\n"
              "    static std::vector<int> v;\n"
              "    return v;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("std::string hello()\n"
              "{\n"
              "     return \"hello\";\n"
              "}\n"
              "\n"
              "std::string &f()\n"
              "{\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:8]: (error) Reference to temporary returned.\n", errout.str());

        // make sure scope is used in function lookup
        check("class Fred {\n"
              "    std::string hello() {\n"
              "        return std::string();\n"
              "    }\n"
              "};\n"
              "std::string &f() {\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("std::string hello() {\n"
              "     return std::string();\n"
              "}\n"
              "\n"
              "std::string &f() {\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:6]: (error) Reference to temporary returned.\n", errout.str());

        check("std::string hello() {\n"
              "     return \"foo\";\n"
              "}\n"
              "\n"
              "std::string &f() {\n"
              "    return hello().substr(1);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("class Foo;\n"
              "Foo hello() {\n"
              "     return Foo();\n"
              "}\n"
              "\n"
              "Foo& f() {\n"
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("[test.cpp:7]: (error) Reference to temporary returned.\n", errout.str());

        // make sure function overloads are handled properly
        check("class Foo;\n"
              "Foo & hello(bool) {\n"
              "     static Foo foo;\n"
              "     return foo;\n"
              "}\n"
              "Foo hello() {\n"
              "     return Foo();\n"
              "}\n"
              "\n"
              "Foo& f() {\n"
              "    return hello(true);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("Foo hello() {\n"
              "     return Foo();\n"
              "}\n"
              "\n"
              "Foo& f() {\n" // Unknown type - might be a reference
              "    return hello();\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }
    void testautovar10() { // #2930 - assignment of function parameter
        check("void foo(char* p) {\n"
              "    p = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) Assignment of function parameter has no effect outside the function.\n", errout.str());

        check("void foo(int b) {\n"
              "    b = foo(b);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) Assignment of function parameter has no effect outside the function.\n", errout.str());

        check("void foo(char* p) {\n"
              "    if (!p) p = buf;\n"
              "    *p = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(char* p) {\n"
              "    if (!p) p = buf;\n"
              "    do_something(p);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(char* p) {\n"
              "    while (!p) p = buf;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(char* p) {\n"
              "    p = 0;\n"
              "    asm(\"somecmd\");\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(Foo* p) {\n"
              "    p = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) Assignment of function parameter has no effect outside the function.\n", errout.str());

        check("class Foo {};\n"
              "void foo(Foo p) {\n"
              "    p = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) Assignment of function parameter has no effect outside the function.\n", errout.str());

        check("void foo(Foo p) {\n"
              "    p = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(int& p) {\n"
              "    p = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("double foo(double d) {\n" // #5005
              "    int i = d;\n"
              "    d = i;\n"
              "    return d;"
              "}",false,false);
        ASSERT_EQUALS("", errout.str());
    }
Exemple #15
0
 void invalid_switches() const {
     const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-a", "-v", "-q"};
     options args(sizeof argv / sizeof argv[0], argv);
     ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
     ASSERT_EQUALS(true, args.quiet());
 }
Exemple #16
0
    void useClosedFile() {
        check("void foo(FILE*& f) {\n"
              "    fclose(f);\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "    clearerr(f);\n"
              "    fread(buffer, 5, 6, f);\n"
              "    ungetc('a', f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Used file that is not opened.\n"
                      "[test.cpp:4]: (error) Used file that is not opened.\n"
                      "[test.cpp:5]: (error) Used file that is not opened.\n"
                      "[test.cpp:6]: (error) Used file that is not opened.\n", errout.str());

        check("void foo(FILE*& f) {\n"
              "    if(!ferror(f)) {\n"
              "        fclose(f);\n"
              "        return;"
              "    }\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(FILE*& f) {\n"
              "    fclose(f);\n"
              "    f = fopen(name, \"r\");\n"
              "    fread(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(FILE*& f) {\n"
              "    f = fopen(name, \"r\");\n"
              "    f = g;\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo() {\n"
              "    FILE* f;\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Used file that is not opened.\n", errout.str());

        check("void foo() {\n"
              "    FILE* f(stdout);\n"
              "    fwrite(buffer, 5, 6, f);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo() {\n" // #3965
              "    FILE* f[3];\n"
              "    f[0] = fopen(name, mode);\n"
              "    fclose(f[0]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // #4368: multiple functions
        check("static FILE *fp = NULL;\n"
              "\n"
              "void close()\n"
              "{\n"
              "  fclose(fp);\n"
              "}\n"
              "\n"
              "void dump()\n"
              "{\n"
              "  if (fp == NULL) return;\n"
              "  fprintf(fp, \"Here's the output.\\n\");\n"
              "}\n"
              "\n"
              "int main()\n"
              "{\n"
              "  fp = fopen(\"test.txt\", \"w\");\n"
              "  dump();\n"
              "  close();\n"
              "  return 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("static FILE *fp = NULL;\n"
              "\n"
              "void close()\n"
              "{\n"
              "  fclose(fp);\n"
              "}\n"
              "\n"
              "void dump()\n"
              "{\n"
              "  fclose(fp);\n"
              "  fprintf(fp, \"Here's the output.\\n\");\n"
              "}");
        ASSERT_EQUALS("[test.cpp:11]: (error) Used file that is not opened.\n", errout.str());

        // #4466
        check("void chdcd_parse_nero(FILE *infile) {\n"
              "    switch (mode) {\n"
              "        case 0x0300:\n"
              "            fclose(infile);\n"
              "            return;\n"
              "        case 0x0500:\n"
              "            fclose(infile);\n"
              "            return;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // #4649
        check("void foo() {\n"
              "    struct {FILE *f1; FILE *f2;} a;\n"
              "    a.f1 = fopen(name,mode);\n"
              "    a.f2 = fopen(name,mode);\n"
              "    fclose(a.f1);\n"
              "    fclose(a.f2);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }
    void returnIssues() {
        check("void* foo(int i) {\n"
              "    return i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an integer in a function with pointer return type is not portable.\n", errout.str());

        check("void* foo(int* i) {\n"
              "    return i;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void* foo() {\n"
              "    return 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int foo(int i) {\n"
              "    return i;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int foo(char* c) {\n"
              "    return c;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());

        check("int foo(char* c) {\n"
              "    return 1+c;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());

        check("std::string foo(char* c) {\n"
              "    return c;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int foo(char *a, char *b) {\n" // #4486
              "    return a + 1 - b;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("struct s {\n" // 4642
              "   int i;\n"
              "};\n"
              "int func(struct s *p) {\n"
              " return 1 + p->i;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("static void __iomem *f(unsigned int port_no) {\n"
              "  void __iomem *mmio = hpriv->mmio;\n"
              "  return mmio + (port_no * 0x80);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // #7247: dont check return statements in nested functions..
        check("int foo() {\n"
              "  struct {\n"
              "    const char * name() { return \"abc\"; }\n"
              "  } table;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // #7451: Lambdas
        check("const int* test(std::vector<int> outputs, const std::string& text) {\n"
              "  auto it = std::find_if(outputs.begin(), outputs.end(), \n"
              "     [&](int ele) { return \"test\" == text; });\n"
              "  return nullptr;\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }
Exemple #18
0
    void testScanfArgument() {
        check("void foo() {\n"
              "    scanf(\"%1d\", &foo);\n"
              "    sscanf(bar, \"%1d\", &foo);\n"
              "    scanf(\"%1u%1u\", &foo, bar());\n"
              "    scanf(\"%*1x %1x %29s\", &count, KeyName);\n" // #3373
              "    fscanf(f, \"%7ms\", &ref);\n" // #3461
              "    sscanf(ip_port, \"%*[^:]:%4d\", &port);\n" // #3468
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo() {\n"
              "    scanf(\"\", &foo);\n"
              "    scanf(\"%1d\", &foo, &bar);\n"
              "    fscanf(bar, \"%1d\", &foo, &bar);\n"
              "    scanf(\"%*1x %1x %29s\", &count, KeyName, foo);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) scanf format string has 0 parameters but 1 are given.\n"
                      "[test.cpp:3]: (warning) scanf format string has 1 parameters but 2 are given.\n"
                      "[test.cpp:4]: (warning) fscanf format string has 1 parameters but 2 are given.\n"
                      "[test.cpp:5]: (warning) scanf format string has 2 parameters but 3 are given.\n", errout.str());

        check("void foo() {\n"
              "    scanf(\"%1d\");\n"
              "    scanf(\"%1u%1u\", bar());\n"
              "    sscanf(bar, \"%1d%1d\", &foo);\n"
              "    scanf(\"%*1x %1x %29s\", &count);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (error) scanf format string has 1 parameters but only 0 are given.\n"
                      "[test.cpp:3]: (error) scanf format string has 2 parameters but only 1 are given.\n"
                      "[test.cpp:4]: (error) sscanf format string has 2 parameters but only 1 are given.\n"
                      "[test.cpp:5]: (error) scanf format string has 2 parameters but only 1 are given.\n", errout.str());

        check("void foo() {\n"
              "    char input[10];\n"
              "    char output[5];\n"
              "    sscanf(input, \"%3s\", output);\n"
              "    sscanf(input, \"%4s\", output);\n"
              "    sscanf(input, \"%5s\", output);\n"
              "}", false);
        ASSERT_EQUALS("[test.cpp:6]: (error) Width 5 given in format string (no. 1) is larger than destination buffer 'output[5]', use %4s to prevent overflowing it.\n", errout.str());

        check("void foo() {\n"
              "    char input[10];\n"
              "    char output[5];\n"
              "    sscanf(input, \"%s\", output);\n"
              "    sscanf(input, \"%3s\", output);\n"
              "    sscanf(input, \"%4s\", output);\n"
              "    sscanf(input, \"%5s\", output);\n"
              "}", true);
        ASSERT_EQUALS("[test.cpp:5]: (warning, inconclusive) Width 3 given in format string (no. 1) is smaller than destination buffer 'output[5]'.\n"
                      "[test.cpp:7]: (error) Width 5 given in format string (no. 1) is larger than destination buffer 'output[5]', use %4s to prevent overflowing it.\n"
                      "[test.cpp:4]: (warning) scanf without field width limits can crash with huge input data.\n", errout.str());

        check("void foo() {\n"
              "    const size_t BUFLENGTH(2048);\n"
              "    typedef char bufT[BUFLENGTH];\n"
              "    bufT line= {0};\n"
              "    bufT projectId= {0};\n"
              "    const int scanrc=sscanf(line, \"Project(\\\"{%36s}\\\")\", projectId);\n"
              "    sscanf(input, \"%5s\", output);\n"
              "}", true);
        ASSERT_EQUALS("", errout.str());
    }
 void suppressionsFileNameWithExtraPath() {
     // Ticket #2797
     Suppressions suppressions;
     suppressions.addSuppression("errorid", "./a.c", 123);
     ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "a.c", 123));
 }
Exemple #20
0
    void testPrintfArgument() {
        check("void foo() {\n"
              "    printf(\"%u\");\n"
              "    printf(\"%u%s\", 123);\n"
              "    printf(\"%u%s%d\", 0, bar());\n"
              "    printf(\"%u%%%s%d\", 0, bar());\n"
              "    printf(\"%udfd%%dfa%s%d\", 0, bar());\n"
              "    fprintf(stderr,\"%u%s\");\n"
              "    snprintf(str,10,\"%u%s\");\n"
              "    sprintf(string1, \"%-*.*s\", 32, string2);\n" // #3364
              "    snprintf(a, 9, \"%s%d\", \"11223344\");\n" // #3655
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (error) printf format string has 1 parameters but only 0 are given.\n"
                      "[test.cpp:3]: (error) printf format string has 2 parameters but only 1 are given.\n"
                      "[test.cpp:4]: (error) printf format string has 3 parameters but only 2 are given.\n"
                      "[test.cpp:5]: (error) printf format string has 3 parameters but only 2 are given.\n"
                      "[test.cpp:6]: (error) printf format string has 3 parameters but only 2 are given.\n"
                      "[test.cpp:7]: (error) fprintf format string has 2 parameters but only 0 are given.\n"
                      "[test.cpp:8]: (error) snprintf format string has 2 parameters but only 0 are given.\n"
                      "[test.cpp:9]: (error) sprintf format string has 3 parameters but only 2 are given.\n"
                      "[test.cpp:10]: (error) snprintf format string has 2 parameters but only 1 are given.\n", errout.str());

        check("void foo(char *str) {\n"
              "    printf(\"\", 0);\n"
              "    printf(\"%u\", 123, bar());\n"
              "    printf(\"%u%s\", 0, bar(), 43123);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) printf format string has 0 parameters but 1 are given.\n"
                      "[test.cpp:3]: (warning) printf format string has 1 parameters but 2 are given.\n"
                      "[test.cpp:4]: (warning) printf format string has 2 parameters but 3 are given.\n", errout.str());

        check("void foo() {\n" // swprintf exists as MSVC extension and as standard function: #4790
              "    swprintf(string1, L\"%u\", 32, string2);\n" // MSVC implementation
              "    swprintf(string1, L\"%s%s\", L\"a\", string2);\n" // MSVC implementation
              "    swprintf(string1, 6, L\"%u\", 32, string2);\n" // Standard implementation
              "    swprintf(string1, 6, L\"%u%s\", 32, string2);\n" // Standard implementation
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) swprintf format string has 1 parameters but 2 are given.\n"
                      "[test.cpp:4]: (warning) swprintf format string has 1 parameters but 2 are given.\n", errout.str());

        check("void foo(char *str) {\n"
              "    printf(\"%u\", 0);\n"
              "    printf(\"%u%s\", 123, bar());\n"
              "    printf(\"%u%s%d\", 0, bar(), 43123);\n"
              "    printf(\"%u%%%s%d\", 0, bar(), 43123);\n"
              "    printf(\"%udfd%%dfa%s%d\", 0, bar(), 43123);\n"
              "    printf(\"%\"PRId64\"\n\", 123);\n"
              "    fprintf(stderr,\"%\"PRId64\"\n\", 123);\n"
              "    snprintf(str,10,\"%\"PRId64\"\n\", 123);\n"
              "    fprintf(stderr, \"error: %m\n\");\n" // #3339
              "    printf(\"string: %.*s\n\", len, string);\n" // #3311
              "    fprintf(stderr, \"%*cText.\n\", indent, ' ');\n" // #3313
              "    sprintf(string1, \"%*\", 32);\n" // #3364
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(char* s, const char* s2, std::string s3, int i) {\n"
              "    printf(\"%s%s\", s, s2);\n"
              "    printf(\"%s\", i);\n"
              "    printf(\"%i%s\", i, i);\n"
              "    printf(\"%s\", s3);\n"
              "    printf(\"%s\", \"s4\");\n"
              "    printf(\"%u\", s);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %s in format string (no. 1) requires a char* given in the argument list.\n"
                      "[test.cpp:4]: (warning) %s in format string (no. 2) requires a char* given in the argument list.\n"
                      "[test.cpp:5]: (warning) %s in format string (no. 1) requires a char* given in the argument list.\n", errout.str());

        check("void foo(const int* cpi, const int ci, int i, int* pi, std::string s) {\n"
              "    printf(\"%n\", cpi);\n"
              "    printf(\"%n\", ci);\n"
              "    printf(\"%n\", i);\n"
              "    printf(\"%n\", pi);\n"
              "    printf(\"%n\", s);\n"
              "    printf(\"%n\", \"s4\");\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
                      "[test.cpp:3]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
                      "[test.cpp:4]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
                      "[test.cpp:6]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
                      "[test.cpp:7]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n", errout.str());

        check("class foo {};\n"
              "void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, unsigned int u) {\n"
              "    printf(\"%X\", f);\n"
              "    printf(\"%c\", \"s4\");\n"
              "    printf(\"%o\", d);\n"
              "    printf(\"%x\", cpi);\n"
              "    printf(\"%o\", b);\n"
              "    printf(\"%X\", bp);\n"
              "    printf(\"%X\", u);\n"
              "    printf(\"%X\", i);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %X in format string (no. 1) requires an integer given in the argument list.\n"
                      "[test.cpp:4]: (warning) %c in format string (no. 1) requires an integer given in the argument list.\n"
                      "[test.cpp:5]: (warning) %o in format string (no. 1) requires an integer given in the argument list.\n", errout.str());

        check("class foo {};\n"
              "void foo(const int* cpi, foo f, bar b, bar* bp, double d, unsigned int u, unsigned char uc) {\n"
              "    printf(\"%i\", f);\n"
              "    printf(\"%d\", \"s4\");\n"
              "    printf(\"%d\", d);\n"
              "    printf(\"%d\", u);\n"
              "    printf(\"%d\", cpi);\n"
              "    printf(\"%i\", b);\n"
              "    printf(\"%i\", bp);\n"
              "    printf(\"%i\", uc);\n" // char is smaller than int, so there shouldn't be a problem
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %i in format string (no. 1) requires a signed integer given in the argument list.\n"
                      "[test.cpp:4]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n"
                      "[test.cpp:5]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n"
                      "[test.cpp:6]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n", errout.str());

        check("class foo {};\n"
              "void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, bool bo) {\n"
              "    printf(\"%u\", f);\n"
              "    printf(\"%u\", \"s4\");\n"
              "    printf(\"%u\", d);\n"
              "    printf(\"%u\", i);\n"
              "    printf(\"%u\", cpi);\n"
              "    printf(\"%u\", b);\n"
              "    printf(\"%u\", bp);\n"
              "    printf(\"%u\", bo);\n" // bool shouldn't have a negative sign
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
                      "[test.cpp:4]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
                      "[test.cpp:5]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
                      "[test.cpp:6]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n", errout.str());

        check("class foo {};\n"
              "void foo(const int* cpi, foo f, bar b, bar* bp, char c) {\n"
              "    printf(\"%p\", f);\n"
              "    printf(\"%p\", c);\n"
              "    printf(\"%p\", bp);\n"
              "    printf(\"%p\", cpi);\n"
              "    printf(\"%p\", b);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %p in format string (no. 1) requires an address given in the argument list.\n"
                      "[test.cpp:4]: (warning) %p in format string (no. 1) requires an address given in the argument list.\n", errout.str());

        check("class foo {};\n"
              "void foo(const int* cpi, foo f, bar b, bar* bp, double d) {\n"
              "    printf(\"%e\", f);\n"
              "    printf(\"%E\", \"s4\");\n"
              "    printf(\"%f\", cpi);\n"
              "    printf(\"%G\", bp);\n"
              "    printf(\"%f\", d);\n"
              "    printf(\"%f\", b);\n"
              "    printf(\"%f\", (float)cpi);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %e in format string (no. 1) requires a floating point number given in the argument list.\n"
                      "[test.cpp:4]: (warning) %E in format string (no. 1) requires a floating point number given in the argument list.\n"
                      "[test.cpp:5]: (warning) %f in format string (no. 1) requires a floating point number given in the argument list.\n"
                      "[test.cpp:6]: (warning) %G in format string (no. 1) requires a floating point number given in the argument list.\n", errout.str());

        check("class foo;\n"
              "void foo(foo f) {\n"
              "    printf(\"%u\", f);\n"
              "    printf(\"%f\", f);\n"
              "    printf(\"%p\", f);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
                      "[test.cpp:4]: (warning) %f in format string (no. 1) requires a floating point number given in the argument list.\n"
                      "[test.cpp:5]: (warning) %p in format string (no. 1) requires an address given in the argument list.\n", errout.str());

        // Ticket #4189 (Improve check (printf("%l") not detected)) tests (according to C99 7.19.6.1.7)
        // False positive tests
        check("void foo(signed char sc, unsigned char uc, short int si, unsigned short int usi) {\n"
              "  printf(\"%hhx %hhd\", sc, uc);\n"
              "  printf(\"%hd %hi\", si, usi);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(long long int lli, unsigned long long int ulli, long int li, unsigned long int uli) {\n"
              "  printf(\"%llo %llx\", lli, ulli);\n"
              "  printf(\"%ld %lu\", li, uli);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(intmax_t im, uintmax_t uim, size_t s, ptrdiff_t p, long double ld) {\n"
              "  printf(\"%jd %jo\", im, uim);\n"
              "  printf(\"%zx\", s);\n"
              "  printf(\"%ti\", p);\n"
              "  printf(\"%La\", ld);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // False negative test
        check("void foo(unsigned int i) {\n"
              "  printf(\"%h\", i);\n"
              "  printf(\"%hh\", i);\n"
              "  printf(\"%l\", i);\n"
              "  printf(\"%ll\", i);\n"
              "  printf(\"%j\", i);\n"
              "  printf(\"%z\", i);\n"
              "  printf(\"%t\", i);\n"
              "  printf(\"%L\", i);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (warning) 'h' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:3]: (warning) 'hh' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:4]: (warning) 'l' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:5]: (warning) 'll' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:6]: (warning) 'j' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:7]: (warning) 'z' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:8]: (warning) 't' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n"
                      "[test.cpp:9]: (warning) 'L' in format string (no. 1) is a length modifier and cannot be used without a conversion specifier.\n", errout.str());
    }
 void suppressionsBadId1() {
     Suppressions suppressions;
     std::istringstream s("123");
     ASSERT_EQUALS("Failed to add suppression. Invalid id \"123\"", suppressions.parseFile(s));
 }
Exemple #22
0
 void which_test_method() const {
     const char* argv[] = {"./test_runner", "TestClass::TestMethod"};
     options args(sizeof argv / sizeof argv[0], argv);
     ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
 }
Exemple #23
0
TEST_F(SyncTailTest, SyncApplyNoNamespaceBadOp) {
    const BSONObj op = BSON("op"
                            << "x");
    ASSERT_OK(SyncTail::syncApply(_txn.get(), op, false, _applyOp, _applyCmd, _incOps));
    ASSERT_EQUALS(0U, _opsApplied);
}
Exemple #24
0
 void no_test_method() const {
     const char* argv[] = {"./test_runner"};
     options args(sizeof argv / sizeof argv[0], argv);
     ASSERT_EQUALS("", args.which_test());
 }
Exemple #25
0
TEST_F(SyncTailTest, MultiApplyReturnsEmptyArrayOperationWhenNoOperationsAreGiven) {
    auto writerPool = SyncTail::makeWriterPool();
    auto status = multiApply(_txn.get(), writerPool.get(), {}, noopApplyOperationFn).getStatus();
    ASSERT_EQUALS(ErrorCodes::EmptyArrayOperation, status);
    ASSERT_STRING_CONTAINS(status.reason(), "no operations provided to multiApply");
}
Exemple #26
0
 void quiet() const {
     const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-q"};
     options args(sizeof argv / sizeof argv[0], argv);
     ASSERT_EQUALS(true, args.quiet());
 }
Exemple #27
0
 void validate(){
     ASSERT_EQUALS(target.take() , nthreads * iterations);
 }
Exemple #28
0
 void multiple_testcases() const {
     const char* argv[] = {"./test_runner", "TestClass::TestMethod", "Ignore::ThisOne"};
     options args(sizeof argv / sizeof argv[0], argv);
     ASSERT_EQUALS("TestClass::TestMethod", args.which_test());
 }
void CTestAsyncSuplLbsApi::TestObserverMultiAppL()
//
// Test the use of MLbsHostSettingsStoreObserver implementations with
// multiple applications/processes.
//
// This test spawns two instances of a separate observer application, each
// of which requests notification of changes to the settings store that
// is modified here. When the notifier calls LbsHostSettingsUpdated() in
// either of the observer apps, they will attempt to rendezvous with this
// process and pass back the notification details for verification.
//
// 1. Spawn observer applications
// 2. Make rendezvous requests to each observer app
// 3. Create store
// 4. Read the initial host settings in from the ini file
// 5. Create a host settings entry using the initial settings
// 6. Retrieve & validate
// 7. Wait for observer apps to rendezvous
// 8. Get new host settings
// 9. Update the previously added entry
// 10. Retrieve and validate
// 11. Wait for observer apps to rendezvous
// 12. Create another settings entry
// 13. Wait for observer apps to rendezvous
// 14. Set second entry as default
// 15. Update second entry
// 16. Wait for observer apps to rendezvous
// 17. Delete first entry
// 18. Wait for observer apps to rendezvous
// 19. Try to update a non-existent entry
// 20. Update a valid entry
// 21. Wait for observer apps to rendezvous
// 22. Try to delete a non-existent entry
// 23. Delete a valid entry
// 24. Wait for observer apps to rendezvous
// 25. Kill observer apps
//
	{
	INFO_PRINTF1(_L("CTestAsyncSuplLbsApi::TestObserverMultiAppL"));

	// 1. Spawn observer applications
	RProcess app1, app2;
	TInt error = app1.Create(KObserverApp, KNullDesC);
	if (error != KErrNone)
		{
		ERR_PRINTF2(_L("Cannot create observer app 1 - error = %d"), error);
		User::Leave(error);
		}

	CleanupClosePushL(app1);

	error = app2.Create(KObserverApp, KNullDesC);
	if (error != KErrNone)
		{
		ERR_PRINTF2(_L("Cannot create observer app 2 - error = %d"), error);
		User::Leave(error);
		}

	CleanupClosePushL(app2);

	// 2. Make rendezvous requests to each observer app
	TRequestStatus status1, status2;
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);
	app1.Resume();
	app2.Resume();

	// 3. Create store
	iLBSHostStore1 = CLbsHostSettingsStore::NewL(KLbsHostSettingsSuplStoreId);

	// 4. Read the initial host settings in from the ini file
	TLbsHostSettingsSupl settings;
	ASSERT_EQUALS(KErrNone, CTestSuplUtils::GetHostSettingFromConfig(iConfig, settings, _L("HostSetting_Default")));
			
	// 5. Create a host settings entry using the initial settings
	TLbsHostSettingsId newSettingId1;
	TInt err = iLBSHostStore1->CreateHostSettings(settings, KLbsHostSettingsTestCreatorId, newSettingId1);	
	ASSERT_EQUALS(KErrNone, err);
	
	// 6. Retrieve & validate
	TLbsHostSettingsSupl settingResult;
	err = iLBSHostStore1->GetHostSettings(newSettingId1, settingResult);	
	ASSERT_EQUALS(err, KErrNone);
	ASSERT_TRUE(CTestSuplUtils::AreSettingsEqual(settings, settingResult));

	// 7. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId1.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId1.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 8. Get new host settings
	TLbsHostSettingsSupl settings2;
	ASSERT_EQUALS(KErrNone, CTestSuplUtils::GetHostSettingFromConfig(iConfig, settings2, _L("HostSetting_Two")));
	
	// 9. Update the previously added entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId1, settings2);	
	ASSERT_EQUALS(err, KErrNone);
	
	// 10. Retrieve and validate
	err = iLBSHostStore1->GetHostSettings(newSettingId1, settingResult);	
	ASSERT_EQUALS(err, KErrNone);
	ASSERT_TRUE(CTestSuplUtils::AreSettingsEqual(settings2, settingResult));

	// 11. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId1.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId1.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 12. Create another settings entry
	TLbsHostSettingsId newSettingId2;
	err = iLBSHostStore1->CreateHostSettings(settings, KLbsHostSettingsTestCreatorId, newSettingId2);	
	ASSERT_EQUALS(KErrNone, err);

	// 13. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId2.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId2.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 14. Set second entry as default. This should not result in any
	// notification since the default host key is part of the metadata space.
	// (If it did then the test under step 16 below would fail since
	// LbsHostSettingsUpdated() would be called with settings
	// ID = 2 [EDefaultHost] but newSettingsId2 = 1.)
	err = iLBSHostStore1->SetDefaultHostSettings(newSettingId2);	
	ASSERT_EQUALS(err, KErrNone);

	// 15. Update second entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId2, settings2);	
	ASSERT_EQUALS(err, KErrNone);

	// 16. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId2.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId2.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 17. Delete first entry
	err = iLBSHostStore1->DeleteHostSettings(newSettingId1);
	ASSERT_EQUALS(err, KErrNone);

	// 18. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId1.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId1.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 19. Try to update a non-existent entry. This shouldn't result in any
	// notification. (If it did then the test under step 21 below would
	// fail since LbsHostSettingsUpdated() would be called with settings
	// ID = newSettingsId1.)
	err = iLBSHostStore1->UpdateHostSettings(newSettingId1, settings);	
	ASSERT_EQUALS(err, KErrNotFound);

	// 20. Update a valid entry
	err = iLBSHostStore1->UpdateHostSettings(newSettingId2, settings);	
	ASSERT_EQUALS(err, KErrNone);

	// 21. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId2.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId2.iUid));
	app1.Rendezvous(status1);
	app2.Rendezvous(status2);

	// 22. Try to delete a non-existent entry. This shouldn't result in any
	// notification. (If it did then the test under step 24 below would
	// fail since LbsHostSettingsUpdated() would be called with settings
	// ID = newSettingsId1.)
	err = iLBSHostStore1->DeleteHostSettings(newSettingId1);	
	ASSERT_EQUALS(err, KErrNotFound);

	// 23. Delete a valid entry
	err = iLBSHostStore1->DeleteHostSettings(newSettingId2);
	ASSERT_EQUALS(err, KErrNone);

	// 24. Wait for observer apps to rendezvous
	User::WaitForRequest(status1);
	User::WaitForRequest(status2);
	ASSERT_EQUALS(status1.Int(), static_cast<TInt>(newSettingId2.iUid));
	ASSERT_EQUALS(status2.Int(), static_cast<TInt>(newSettingId2.iUid));

	// 25. Kill observer apps
	app1.Kill(KErrNone);
	app2.Kill(KErrNone);

	CleanupStack::PopAndDestroy(&app2);	
	CleanupStack::PopAndDestroy(&app1);	
	}
Exemple #30
0
 void isDirectory() const {
     ASSERT_EQUALS(false, FileLister::isDirectory("readme.txt"));
     ASSERT_EQUALS(true, FileLister::isDirectory("lib"));
 }