Skip to content

Commit

Permalink
Add tests for folly/lang/Hint.h
Browse files Browse the repository at this point in the history
Summary: In preparation of the addition of a new hint.

Reviewed By: Orvid

Differential Revision: D45059034

fbshipit-source-id: 27f640dfbba2577de1e9708d9866395e66dce04f
  • Loading branch information
David Goldblatt authored and facebook-github-bot committed Apr 21, 2023
1 parent bab7716 commit fc8514e
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions folly/lang/test/HintTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// We don't bother with "real" checking of any sort of difference in generated
// code (which would be hard to test, can vary depending on build mode, etc.).
// It's still valuable to have some syntax checking (in the sense of making sure
// various calls compile as expected without warning) in one reduced place.

#include <folly/lang/Hint.h>

#include <folly/portability/GTest.h>

using namespace folly;

TEST(Hint, CompilerMayUnsafelyAssume) {
compiler_may_unsafely_assume(true);
bool falseValue = false;
compiler_must_not_predict(falseValue);
if (falseValue) {
compiler_may_unsafely_assume(false);
}
}

TEST(Hint, CompilerMayUnsafelyAssumeUnreachable) {
bool falseValue = false;
compiler_must_not_predict(falseValue);
if (falseValue) {
compiler_may_unsafely_assume_unreachable();
}
}

struct NonTriviallyCopyable {
NonTriviallyCopyable() {}
NonTriviallyCopyable(const NonTriviallyCopyable&) {}

NonTriviallyCopyable& operator=(const NonTriviallyCopyable&) { return *this; }
int data = 123;
};

TEST(Hint, CompilerMustNotElide) {
int x = 123;
compiler_must_not_elide(x);
NonTriviallyCopyable ntc;
compiler_must_not_elide(ntc);
}

TEST(Hint, CompilerMustNotPredict) {
int x = 123;
compiler_must_not_predict(x);
NonTriviallyCopyable ntc;
compiler_must_not_predict(ntc);
}

0 comments on commit fc8514e

Please sign in to comment.