[vlc-commits] [Git][videolan/vlc][master] 3 commits: adaptive: test: use std::unique_ptr

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Tue Mar 22 09:12:06 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
5c8ad2c3 by Alexandre Janniaux at 2022-03-22T09:46:57+01:00
adaptive: test: use std::unique_ptr

std::unique_ptr use RAII to release the pointers, removing the
requirement of `delete` in the catch{} block. They also help ensuring
error caught are from the tested code and not the test itself.

- - - - -
f3b27b08 by Alexandre Janniaux at 2022-03-22T09:46:57+01:00
adaptive: test: inline try{} and reindent

Now that catch{} is mostly doing nothing, and doesn't require the
segmentList pointers to be visible, we can align the whole function as a
try-catch function block, removing a useless indentation.

No functional changes.

- - - - -
ff0e681f by Alexandre Janniaux at 2022-03-22T09:46:57+01:00
adaptive: test: use unique_ptr for Segment

This ensure that memory leaks are not coming from the test, but from the
tested code instead.

Refs #26728

- - - - -


1 changed file:

- modules/demux/adaptive/test/playlist/SegmentList.cpp


Changes:

=====================================
modules/demux/adaptive/test/playlist/SegmentList.cpp
=====================================
@@ -31,219 +31,211 @@
 #include "../test.hpp"
 
 #include <limits>
+#include <memory>
 
 using namespace adaptive;
 using namespace adaptive::playlist;
 
-int SegmentList_test()
+int SegmentList_test() try
 {
-    SegmentList *segmentList = nullptr;
-    SegmentList *segmentList2 = nullptr;
-    try
+    std::unique_ptr<SegmentList> segmentList;
+    std::unique_ptr<SegmentList> segmentList2;
+    segmentList = std::make_unique<SegmentList>(nullptr);
+    Timescale timescale(100);
+    segmentList->addAttribute(new TimescaleAttr(timescale));
+    /* Check failures */
+    Expect(segmentList->getStartSegmentNumber() == std::numeric_limits<uint64_t>::max());
+    Expect(segmentList->getTotalLength() == 0);
+    uint64_t number; bool discont;
+    Expect(segmentList->getSegmentNumberByTime(1, &number) == false);
+    Expect(segmentList->getMediaSegment(0) == nullptr);
+    Expect(segmentList->getNextMediaSegment(0, &number, &discont) == nullptr);
+    Expect(segmentList->getMinAheadTime(0) == 0);
+    vlc_tick_t time, duration;
+    Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(0, &time, &duration) == false);
+
+    /* Simple elements list */
+    const stime_t START = 1337;
+    std::unique_ptr<Segment> seg = std::make_unique<Segment>(nullptr);
+    seg->setSequenceNumber(123);
+    seg->startTime.Set(START);
+    seg->duration.Set(100);
+    segmentList->addSegment(seg.release());
+
+    Expect(segmentList->getTotalLength() == 100);
+    Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(0), &number) == false);
+    Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(START), &number) == true);
+    Expect(number == 123);
+    Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(123, &time, &duration) == true);
+    Expect(time == timescale.ToTime(START));
+    Expect(duration == timescale.ToTime(100));
+    Segment *segptr = segmentList->getMediaSegment(123);
+    Expect(segptr);
+    Expect(segptr->getSequenceNumber() == 123);
+    Expect(segptr->startTime.Get() == START);
+    segptr = segmentList->getNextMediaSegment(123, &number, &discont);
+    Expect(segptr);
+    Expect(number == 123);
+    Expect(!discont);
+    segptr = segmentList->getNextMediaSegment(122, &number, &discont);
+    Expect(segptr);
+    Expect(number == 123);
+    Expect(discont);
+    Expect(segmentList->getMinAheadTime(0) == timescale.ToTime(100));
+    Expect(segmentList->getMinAheadTime(123) == timescale.ToTime(0));
+
+    for(int i=1; i<10; i++)
     {
-        segmentList = new SegmentList(nullptr);
-        Timescale timescale(100);
-        segmentList->addAttribute(new TimescaleAttr(timescale));
-        /* Check failures */
-        Expect(segmentList->getStartSegmentNumber() == std::numeric_limits<uint64_t>::max());
-        Expect(segmentList->getTotalLength() == 0);
-        uint64_t number; bool discont;
-        Expect(segmentList->getSegmentNumberByTime(1, &number) == false);
-        Expect(segmentList->getMediaSegment(0) == nullptr);
-        Expect(segmentList->getNextMediaSegment(0, &number, &discont) == nullptr);
-        Expect(segmentList->getMinAheadTime(0) == 0);
-        vlc_tick_t time, duration;
-        Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(0, &time, &duration) == false);
-
-        /* Simple elements list */
-        const stime_t START = 1337;
-        Segment *seg = new Segment(nullptr);
-        seg->setSequenceNumber(123);
-        seg->startTime.Set(START);
+        std::unique_ptr<Segment> seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
         seg->duration.Set(100);
-        segmentList->addSegment(seg);
-
-        Expect(segmentList->getTotalLength() == 100);
-        Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(0), &number) == false);
-        Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(START), &number) == true);
-        Expect(number == 123);
-        Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(123, &time, &duration) == true);
-        Expect(time == timescale.ToTime(START));
-        Expect(duration == timescale.ToTime(100));
-        seg = segmentList->getMediaSegment(123);
-        Expect(seg);
-        Expect(seg->getSequenceNumber() == 123);
-        Expect(seg->startTime.Get() == START);
-        seg = segmentList->getNextMediaSegment(123, &number, &discont);
-        Expect(seg);
-        Expect(number == 123);
-        Expect(!discont);
-        seg = segmentList->getNextMediaSegment(122, &number, &discont);
-        Expect(seg);
-        Expect(number == 123);
-        Expect(discont);
-        Expect(segmentList->getMinAheadTime(0) == timescale.ToTime(100));
-        Expect(segmentList->getMinAheadTime(123) == timescale.ToTime(0));
-
-        for(int i=1; i<10; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList->addSegment(seg);
-        }
-
-        Expect(segmentList->getTotalLength() == 100 * 10);
-        Expect(segmentList->getMinAheadTime(123) == timescale.ToTime(100 * 9));
-        Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(START + 100*9 - 1), &number) == true);
-        Expect(number == 123 + 8);
-        Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(123 + 8, &time, &duration) == true);
-        Expect(time == timescale.ToTime(START + 100 * 8));
-        Expect(duration == timescale.ToTime(100));
-        Expect(segmentList->getMinAheadTime(123+8) == timescale.ToTime(100));
-
-        /* merge */
-        segmentList2 = new SegmentList(nullptr);
-        for(int i=5; i<20; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList2->addSegment(seg);
-        }
-        segmentList->updateWith(segmentList2);
-        Expect(segmentList->getStartSegmentNumber() == 123 + 5);
-        Expect(segmentList->getTotalLength() == 100 * 15);
-
-        for(int i=5; i<20; i++)
-        {
-            seg = segmentList->getMediaSegment(123 + i);
-            Expect(seg);
-            Expect(seg->getSequenceNumber() == (uint64_t) 123 + i);
-            Expect(seg->startTime.Get() == START + 100 * i);
-            Expect(seg->duration.Get() == 100);
-        }
-
-        /* prune */
-        segmentList->pruneByPlaybackTime(timescale.ToTime(START+100*6));
-        Expect(segmentList->getStartSegmentNumber() == 123 + 6);
-        Expect(segmentList->getTotalLength() == 100 * 14);
-
-        segmentList->pruneBySegmentNumber(123+10);
-        Expect(segmentList->getStartSegmentNumber() == 123 + 10);
-        Expect(segmentList->getTotalLength() == 100 * 10);
-
-        delete segmentList;
-        delete segmentList2;
-        segmentList2 = nullptr;
-
-        /* gap updates, relative timings */
-        segmentList = new SegmentList(nullptr, true);
-        segmentList->addAttribute(new TimescaleAttr(timescale));
-        segmentList->addAttribute(new DurationAttr(100));
-        Expect(segmentList->inheritDuration());
-        for(int i=0; i<2; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList->addSegment(seg);
-        }
-        segmentList2 = new SegmentList(nullptr, true);
-        for(int i=0; i<2; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(128 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList2->addSegment(seg);
-        }
-        segmentList->updateWith(segmentList2);
-        Expect(segmentList->getStartSegmentNumber() == 128);
-        Expect(segmentList->getSegments().size() == 2);
-        Expect(segmentList->getSegments().at(0)->getSequenceNumber() == 128);
-        Expect(segmentList->getSegments().at(1)->getSequenceNumber() == 129);
-        Expect(segmentList->getSegments().at(0)->startTime.Get() == START + 100 * (128 - 123));
-        Expect(segmentList->getSegments().at(1)->startTime.Get() == START + 100 * (129 - 123));
-
-        delete segmentList;
-        delete segmentList2;
-        segmentList2 = nullptr;
-
-        /* gap updates, absolute media timings */
-        segmentList = new SegmentList(nullptr, false);
-        segmentList->addAttribute(new TimescaleAttr(timescale));
-        segmentList->addAttribute(new DurationAttr(999));
-        Expect(segmentList->inheritDuration());
-        for(int i=0; i<2; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList->addSegment(seg);
-        }
-        segmentList2 = new SegmentList(nullptr, false);
-        for(int i=5; i<7; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList2->addSegment(seg);
-        }
-        segmentList->updateWith(segmentList2);
-        Expect(segmentList->getStartSegmentNumber() == 128);
-        Expect(segmentList->getSegments().size() == 2);
-        Expect(segmentList->getSegments().at(0)->getSequenceNumber() == 128);
-        Expect(segmentList->getSegments().at(1)->getSequenceNumber() == 129);
-        Expect(segmentList->getSegments().at(0)->startTime.Get() == START + 100 * (128 - 123));
-        Expect(segmentList->getSegments().at(1)->startTime.Get() == START + 100 * (129 - 123));
-
-        delete segmentList;
-        delete segmentList2;
-        segmentList2 = nullptr;
-
-        /* Tricky now, check timelined */
-        segmentList = new SegmentList(nullptr);
-        segmentList->addAttribute(new TimescaleAttr(timescale));
-        for(int i=0; i<10; i++)
-        {
-            seg = new Segment(nullptr);
-            seg->setSequenceNumber(123 + i);
-            seg->startTime.Set(START + 100 * i);
-            seg->duration.Set(100);
-            segmentList->addSegment(seg);
-        }
-        const std::vector<Segment*>&allsegments = segmentList->getSegments();
-
-        SegmentTimeline *timeline = new SegmentTimeline(nullptr);
-        segmentList->addAttribute(timeline);
-        timeline->addElement(44, 100, 4, START);
-        Expect(timeline->getTotalLength() == 5 * 100);
-        Expect(segmentList->getStartSegmentNumber() == 44);
-        Expect(segmentList->getTotalLength() == timeline->getTotalLength());
-        seg = segmentList->getMediaSegment(44 + 2);
-        Expect(seg);
-        Expect(seg == allsegments.at(0));
-        Expect(segmentList->getMediaSegment(44 + 6) == nullptr); /* restricted window */
-
-        timeline->addElement(44 + 5, 100, 1, START + 5*100);
-        Expect(timeline->getTotalLength() == 7 * 100);
-        seg = segmentList->getMediaSegment(44 + 6);
-        Expect(seg);
-        Expect(seg == allsegments.at(1));
-
-        delete segmentList;
-
-    } catch (...) {
-        delete segmentList;
-        delete segmentList2;
-        return 1;
+        segmentList->addSegment(seg.release());
     }
 
+    Expect(segmentList->getTotalLength() == 100 * 10);
+    Expect(segmentList->getMinAheadTime(123) == timescale.ToTime(100 * 9));
+    Expect(segmentList->getSegmentNumberByTime(timescale.ToTime(START + 100*9 - 1), &number) == true);
+    Expect(number == 123 + 8);
+    Expect(segmentList->getPlaybackTimeDurationBySegmentNumber(123 + 8, &time, &duration) == true);
+    Expect(time == timescale.ToTime(START + 100 * 8));
+    Expect(duration == timescale.ToTime(100));
+    Expect(segmentList->getMinAheadTime(123+8) == timescale.ToTime(100));
+
+    /* merge */
+    segmentList2 = std::make_unique<SegmentList>(nullptr);
+    for(int i=5; i<20; i++)
+    {
+        std::unique_ptr<Segment> seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList2->addSegment(seg.release());
+    }
+    segmentList->updateWith(segmentList2.get());
+    Expect(segmentList->getStartSegmentNumber() == 123 + 5);
+    Expect(segmentList->getTotalLength() == 100 * 15);
+
+    for(int i=5; i<20; i++)
+    {
+        segptr = segmentList->getMediaSegment(123 + i);
+        Expect(segptr);
+        Expect(segptr->getSequenceNumber() == (uint64_t) 123 + i);
+        Expect(segptr->startTime.Get() == START + 100 * i);
+        Expect(segptr->duration.Get() == 100);
+    }
+
+    /* prune */
+    segmentList->pruneByPlaybackTime(timescale.ToTime(START+100*6));
+    Expect(segmentList->getStartSegmentNumber() == 123 + 6);
+    Expect(segmentList->getTotalLength() == 100 * 14);
+
+    segmentList->pruneBySegmentNumber(123+10);
+    Expect(segmentList->getStartSegmentNumber() == 123 + 10);
+    Expect(segmentList->getTotalLength() == 100 * 10);
+
+    segmentList.reset();
+    segmentList2.reset();
+
+    /* gap updates, relative timings */
+    segmentList = std::make_unique<SegmentList>(nullptr, true);
+    segmentList->addAttribute(new TimescaleAttr(timescale));
+    segmentList->addAttribute(new DurationAttr(100));
+    Expect(segmentList->inheritDuration());
+    for(int i=0; i<2; i++)
+    {
+        seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList->addSegment(seg.release());
+    }
+    segmentList2 = std::make_unique<SegmentList>(nullptr, true);
+    for(int i=0; i<2; i++)
+    {
+        seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(128 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList2->addSegment(seg.release());
+    }
+    segmentList->updateWith(segmentList2.get());
+    Expect(segmentList->getStartSegmentNumber() == 128);
+    Expect(segmentList->getSegments().size() == 2);
+    Expect(segmentList->getSegments().at(0)->getSequenceNumber() == 128);
+    Expect(segmentList->getSegments().at(1)->getSequenceNumber() == 129);
+    Expect(segmentList->getSegments().at(0)->startTime.Get() == START + 100 * (128 - 123));
+    Expect(segmentList->getSegments().at(1)->startTime.Get() == START + 100 * (129 - 123));
+
+    segmentList.reset();
+    segmentList2.reset();
+
+    /* gap updates, absolute media timings */
+    segmentList = std::make_unique<SegmentList>(nullptr, false);
+    segmentList->addAttribute(new TimescaleAttr(timescale));
+    segmentList->addAttribute(new DurationAttr(999));
+    Expect(segmentList->inheritDuration());
+    for(int i=0; i<2; i++)
+    {
+        seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList->addSegment(seg.release());
+    }
+    segmentList2 = std::make_unique<SegmentList>(nullptr, false);
+    for(int i=5; i<7; i++)
+    {
+        seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList2->addSegment(seg.release());
+    }
+    segmentList->updateWith(segmentList2.get());
+    Expect(segmentList->getStartSegmentNumber() == 128);
+    Expect(segmentList->getSegments().size() == 2);
+    Expect(segmentList->getSegments().at(0)->getSequenceNumber() == 128);
+    Expect(segmentList->getSegments().at(1)->getSequenceNumber() == 129);
+    Expect(segmentList->getSegments().at(0)->startTime.Get() == START + 100 * (128 - 123));
+    Expect(segmentList->getSegments().at(1)->startTime.Get() == START + 100 * (129 - 123));
+
+    segmentList.reset();
+    segmentList2.reset();
+
+    /* Tricky now, check timelined */
+    segmentList = std::make_unique<SegmentList>(nullptr);
+    segmentList->addAttribute(new TimescaleAttr(timescale));
+    for(int i=0; i<10; i++)
+    {
+        seg = std::make_unique<Segment>(nullptr);
+        seg->setSequenceNumber(123 + i);
+        seg->startTime.Set(START + 100 * i);
+        seg->duration.Set(100);
+        segmentList->addSegment(seg.release());
+    }
+    const std::vector<Segment*>&allsegments = segmentList->getSegments();
+
+    SegmentTimeline *timeline = new SegmentTimeline(nullptr);
+    segmentList->addAttribute(timeline);
+    timeline->addElement(44, 100, 4, START);
+    Expect(timeline->getTotalLength() == 5 * 100);
+    Expect(segmentList->getStartSegmentNumber() == 44);
+    Expect(segmentList->getTotalLength() == timeline->getTotalLength());
+    segptr = segmentList->getMediaSegment(44 + 2);
+    Expect(segptr);
+    Expect(segptr == allsegments.at(0));
+    Expect(segmentList->getMediaSegment(44 + 6) == nullptr); /* restricted window */
+
+    timeline->addElement(44 + 5, 100, 1, START + 5*100);
+    Expect(timeline->getTotalLength() == 7 * 100);
+    segptr = segmentList->getMediaSegment(44 + 6);
+    Expect(segptr);
+    Expect(segptr == allsegments.at(1));
+
+    segmentList.reset();
+
     return 0;
+} catch (...) {
+    return 1;
 }



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/96621930fc49b8e11aaf4e5fb6320495b69123a3...ff0e681fedb81895e28bb97931c96f757110d099

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/96621930fc49b8e11aaf4e5fb6320495b69123a3...ff0e681fedb81895e28bb97931c96f757110d099
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list