Files
platform-external-webrtc/rtc_base/memory/always_valid_pointer_unittest.cc
Christoffer Jansson 25361a89dd Revert "Reland "Don't create PacketSocketFactory inside BasicPortAllocatorSession""
This reverts commit a8be79ce27f10a698bdb46e490c2bbcbb2300e52.

Reason for revert: Downstream projects were not fixed and I was to eager to reland this, sorry about this.

Original change's description:
> Reland "Don't create PacketSocketFactory inside BasicPortAllocatorSession"
>
> This is a reland of commit 7d4634cef76a1ac244d4b83faaf4c617bf236b71
>
> Original change's description:
> > Don't create PacketSocketFactory inside BasicPortAllocatorSession
> >
> > This extends AlwaysValidPointer to avoid creating a unique_ptr inside it.
> >
> > Bug: webrtc:13145
> > Change-Id: I73a4f18d0a7037b57f575b04b134e4f7eadceb79
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/263240
> > Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
> > Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
> > Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> > Cr-Commit-Position: refs/heads/main@{#37048}
>
> Bug: webrtc:13145
> Change-Id: I7d64c25b2942b392a1c35ff2fe1edc83d7b03746
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264503
> Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> Reviewed-by: Christoffer Jansson <jansson@webrtc.org>
> Commit-Queue: Christoffer Jansson <jansson@webrtc.org>
> Reviewed-by: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
> Cr-Commit-Position: refs/heads/main@{#37088}

Bug: webrtc:13145
Change-Id: Ie7990bae9a7c864ffaa4eb5b637618caad509633
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264823
Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Christoffer Jansson <jansson@webrtc.org>
Owners-Override: Christoffer Jansson <jansson@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37089}
2022-06-02 09:45:16 +00:00

94 lines
2.8 KiB
C++

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/memory/always_valid_pointer.h"
#include <string>
#include "test/gtest.h"
namespace webrtc {
TEST(AlwaysValidPointerTest, DefaultToEmptyValue) {
AlwaysValidPointer<std::string> ptr(nullptr);
EXPECT_EQ(*ptr, "");
}
TEST(AlwaysValidPointerTest, DefaultWithForwardedArgument) {
AlwaysValidPointer<std::string> ptr(nullptr, "test");
EXPECT_EQ(*ptr, "test");
}
TEST(AlwaysValidPointerTest, DefaultToSubclass) {
struct A {
virtual ~A() {}
virtual int f() = 0;
};
struct B : public A {
int b = 0;
explicit B(int val) : b(val) {}
virtual ~B() {}
int f() override { return b; }
};
AlwaysValidPointer<A, B> ptr(nullptr, 3);
EXPECT_EQ(ptr->f(), 3);
EXPECT_EQ((*ptr).f(), 3);
EXPECT_EQ(ptr.get()->f(), 3);
}
TEST(AlwaysValidPointerTest, NonDefaultValue) {
std::string str("keso");
AlwaysValidPointer<std::string> ptr(&str, "test");
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipOfInstance) {
std::string str("keso");
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("kent");
AlwaysValidPointer<std::string> ptr(std::move(str2), &str);
EXPECT_EQ(*ptr, "kent");
EXPECT_EQ(str2, nullptr);
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipFallbackOnPointer) {
std::string str("keso");
std::unique_ptr<std::string> str2;
AlwaysValidPointer<std::string> ptr(std::move(str2), &str);
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipFallbackOnDefault) {
std::unique_ptr<std::string> str;
std::string* str_ptr = nullptr;
AlwaysValidPointer<std::string> ptr(std::move(str), str_ptr);
EXPECT_EQ(*ptr, "");
}
TEST(AlwaysValidPointerTest,
TakeOverOwnershipFallbackOnDefaultWithForwardedArgument) {
std::unique_ptr<std::string> str2;
AlwaysValidPointer<std::string> ptr(std::move(str2), nullptr, "keso");
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipDoesNotForwardDefaultArguments) {
std::unique_ptr<std::string> str = std::make_unique<std::string>("kalle");
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("anka");
AlwaysValidPointer<std::string> ptr(std::move(str), nullptr, *str2);
EXPECT_EQ(*ptr, "kalle");
EXPECT_TRUE(!str);
EXPECT_EQ(*str2, "anka");
}
TEST(AlwaysValidPointerTest, DefaultToLambda) {
AlwaysValidPointer<std::string> ptr(
nullptr, []() { return std::make_unique<std::string>("onkel skrue"); });
EXPECT_EQ(*ptr, "onkel skrue");
}
} // namespace webrtc